Service与Intent(一)
2016-05-14 10:11
501 查看
Fancis学习笔记二
-------------------Service与Intent(一)
Where there is a will, there is a way!
1.Log输出学习;
2.Service与Intent学习;
3.Service生命周期;
4.adb安装APK;
一、Service学习根据sundy视频总结及网上资料查找
service不是一个独立的进程,默认情况下运行在应用程序的主线程中。
启动服务后,按Home键,服务还会执行。
启动服务后,退出进程,服务不会执行。
启动服务,不用多线程,界面会阻塞。
Service的分类:
按照启动方式分类
(一)Started
上个图:
![](https://img-blog.csdn.net/20160514112007988?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
1、startService()来启动。
2、一旦启动,就运行在后台,即便启动它的对象都销毁了。
3、通常只启动,不返回值。
4、通常网络上传或下载,操作完成后,自动停止。
5、onStartCommand()
(二)Bound
1、bindService()来启动
2、提供客户端服务器接口来启动
3、发送请求、得到返回值,甚至通过IPC来通讯
4、使用者绑定,只要一个绑定者,服务运行,所有绑定者都退出,服务退出。
5、onBind()
Service生命周期:
onCreate()
onDestory()
onStartCommand()
onBind()
Service只有在系统资源不足的时候才有可能被系统销毁。值得注意的是:Service如果正在bind状态,很少被销毁,如果服务运行在前台,几乎不会被销毁。
创建和使用Service步骤:
1、继承Service类实现自己的服务
2、在Mainifest.xml中注册服务
3、启动服务startService()
4、停止服务 stopService()、stopSelf()
绑定Service的步骤
1、继承Service类或者IntentService类实现自己的服务。
2、在Mainifest.xml中注册服务
3、绑定服务bindService()
4、取消绑定服务unbinService()
第二类型service参考本链接:http://blog.csdn.net/blueskydyliu/article/details/6741223
二、Android-StartService和BoundService的区别
1. 两者与Activity的关系
StartService和启动它的Activity不同生共死,进程关闭了,该服务类型不会关闭
BoundService和启动它的Activity同生共死,进程关闭了,该服务类型会关闭
2 .多次start 或者 bind观察service的实例化的数量
两种方式,只要没有被实例化,都会被create,
但是多次的实例化时:start每次都会onstartcommand。
而bind,则每次都不调用onBind
3 .音乐播放器的例子
使用startService的原因:
1 )不与进程同生共死
2 )每次都启动onStartCommand
不使用BoundService的原因:
1 )与进程同生共死
2 )只能绑定一次
4 两者最本质区别
BoundService:其实有一个客户端和服务器端的概念,,借助binder来通信。
源码一:
第一个类:MainActivity
第二个类:TestService
三、Service生命周期
上图:
![](https://img-blog.csdn.net/20160514112302956?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
四、adb安装APK,adb install APK本地目录
更多内容会在后续更新,小白正在努力学习中,感谢同各位分享!
-------------------Service与Intent(一)
Where there is a will, there is a way!
1.Log输出学习;
2.Service与Intent学习;
3.Service生命周期;
4.adb安装APK;
一、Service学习根据sundy视频总结及网上资料查找
service不是一个独立的进程,默认情况下运行在应用程序的主线程中。
启动服务后,按Home键,服务还会执行。
启动服务后,退出进程,服务不会执行。
启动服务,不用多线程,界面会阻塞。
Service的分类:
按照启动方式分类
(一)Started
上个图:
1、startService()来启动。
2、一旦启动,就运行在后台,即便启动它的对象都销毁了。
3、通常只启动,不返回值。
4、通常网络上传或下载,操作完成后,自动停止。
5、onStartCommand()
(二)Bound
1、bindService()来启动
2、提供客户端服务器接口来启动
3、发送请求、得到返回值,甚至通过IPC来通讯
4、使用者绑定,只要一个绑定者,服务运行,所有绑定者都退出,服务退出。
5、onBind()
Service生命周期:
onCreate()
onDestory()
onStartCommand()
onBind()
Service只有在系统资源不足的时候才有可能被系统销毁。值得注意的是:Service如果正在bind状态,很少被销毁,如果服务运行在前台,几乎不会被销毁。
创建和使用Service步骤:
1、继承Service类实现自己的服务
2、在Mainifest.xml中注册服务
3、启动服务startService()
4、停止服务 stopService()、stopSelf()
绑定Service的步骤
1、继承Service类或者IntentService类实现自己的服务。
2、在Mainifest.xml中注册服务
3、绑定服务bindService()
4、取消绑定服务unbinService()
第二类型service参考本链接:http://blog.csdn.net/blueskydyliu/article/details/6741223
二、Android-StartService和BoundService的区别
1. 两者与Activity的关系
StartService和启动它的Activity不同生共死,进程关闭了,该服务类型不会关闭
BoundService和启动它的Activity同生共死,进程关闭了,该服务类型会关闭
2 .多次start 或者 bind观察service的实例化的数量
两种方式,只要没有被实例化,都会被create,
但是多次的实例化时:start每次都会onstartcommand。
而bind,则每次都不调用onBind
3 .音乐播放器的例子
使用startService的原因:
1 )不与进程同生共死
2 )每次都启动onStartCommand
不使用BoundService的原因:
1 )与进程同生共死
2 )只能绑定一次
4 两者最本质区别
BoundService:其实有一个客户端和服务器端的概念,,借助binder来通信。
源码一:
第一个类:MainActivity
package myapplication.com.servicedemo; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Intent _intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.startButton); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { _intent = new Intent(MainActivity.this, TestService.class); MainActivity.this.startService(_intent); } }); Button bt2 = (Button) findViewById(R.id.stopbutton); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (_intent != null) { MainActivity.this.stopService(_intent); Log.i("stopService", "print service"); } } }); } }
第二个类:TestService
package myapplication.com.servicedemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log; /** * Created by Administrator on 2016/5/14. */ public class TestService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } 4000 @Override public int onStartCommand(Intent intent, int flags, int startId) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) { Log.i("servicetest", "print number:" + i); } } }).start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("on Destroy", "Destroy"); } }activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="myapplication.com.servicedemo.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/startButton" android:text="startService" android:layout_marginTop="167dp" android:layout_below="@+id/textView" android:layout_toRightOf="@+id/textView" android:layout_toEndOf="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stopbutton" android:text="stopService" android:layout_below="@+id/startButton" android:layout_alignRight="@+id/startButton" android:layout_alignEnd="@+id/startButton" android:layout_marginTop="58dp" /> </RelativeLayout>AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="myapplication.com.servicedemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".TestService" /> </application> </manifest>
三、Service生命周期
上图:
四、adb安装APK,adb install APK本地目录
更多内容会在后续更新,小白正在努力学习中,感谢同各位分享!
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories