Android四大组件 — Service
本地服务(LocalService)
调用者和service在同一个进程里,所以运行在主进程的main线程中。所以不能进行耗时操作,可以采用在service里面创建一个Thread来执行任务。service影响的是进程的生命周期,讨论与Thread的区别没有意义。
任何 Activity 都可以控制同一 Service,而系统也只会创建一个对应 Service 的实例。|
两种启动方式
第一种启动方式:
通过start方式开启服务.
使用service的步骤:
1,定义一个类继承service
2,manifest.xml文件中配置service
3,使用context的startService(Intent)方法启动service
4,不在使用时,调用stopService(Intent)方法停止服务
使用start方式启动的生命周期:
onCreate() – > onStartCommand() – > onDestory()
注意:如果服务已经开启,不会重复回调onCreate()方法,如果再次调用context.startService()方法,service而是会调用onStart()或者onStartCommand()方法。停止服务需要调用context.stopService()方法,服务停止的时候回调onDestory被销毁。
特点:
一旦服务开启就跟调用者(开启者)没有任何关系了。开启者退出了,开启者挂了,服务还在后台长期的运行,开启者不能调用服务里面的方法。
第二种启动方式
采用bind的方式开启服务
使用service的步骤:
1,定义一个类继承Service
2,在manifest.xml文件中注册service
3,使用context的bindService(Intent,ServiceConnection,int)方法启动service
4,不再使用时,调用unbindService(ServiceConnection)方法停止该服务
使用这种bind方式启动的service的生命周期如下:
onCreate() – > onBind() --> onUnbind() – > onDestory()
注意:绑定服务不会调用onStart()或者onStartCommand()方法
特点:bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。绑定者可以调用服务里面的方法。
示例:
定义一个类继承service
//本地service不涉及进程间通信
public class MyService extends Service {
private String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG,"onCreate");
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG,"onStart");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
//绑定服务时调用这个方法,返回一个IBinder对象
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG,"onBind");
return new MyBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,"onUnbind");
return super.onUnbind(intent);
}
// 停止服务,通过调用Context.unbindService(),别忘了service也继承了Context类
// @Override
// public void unbindService(ServiceConnection conn) {
// super.unbindService(conn);
// Log.i(TAG,"unbindService");
// }
//服务挂了
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG,"onDestroy");
}
public interface MyIBinder{
void invokeMethodInMyService();
}
public class MyBinder extends Binder implements MyIBinder{
public void stopService(ServiceConnection serviceConnection){
unbindService(serviceConnection);
}
@Override
public void invokeMethodInMyService() {
for(int i =0; i < 20; i ++){
System.out.println("service is opening");
}
}
}
在manifest.xml文件中注册service
//Service 必须要注册
<service android:name=".server.MyService"
android:exported="true">
<intent-filter>
<action android:name="cn.hotwoo.play.server.MyService"/>
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
绑定自定义的service
public class CustomActivity extends AppCompatActivity {
private Button startService, unbindService;
private MyService.MyBinder myBinder;
private ServiceConnection serviceConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);
startService = (Button) findViewById(R.id.service_start);
unbindService = (Button) findViewById(R.id.unbind_service);
startService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// startService(new Intent(CustomActivity.this, MyService.class));
serviceConnection = new MyServiceConnection();
bindService(new Intent(CustomActivity.this, MyService.class), serviceConnection, Context.BIND_AUTO_CREATE);
}
});
unbindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(serviceConnection);
}
});
}
class MyServiceConnection implements ServiceConnection {
//这里的第二个参数IBinder就是Service中的onBind方法返回的
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("MyService", "onServiceConnected");
myBinder = (MyService.MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("MyService", "onServiceDisconnected");
}
}
}
startService输出日志:
04-01 19:56:09.846 22845-22845/cn.hotwoo.play I/MyService: onCreate
04-01 19:56:09.854 22845-22845/cn.hotwoo.play I/MyService: onStartCommand
04-01 19:56:09.854 22845-22845/cn.hotwoo.play I/MyService: onStart
bindService 输出日志:
04-01 19:53:21.459 14704-14704/cn.hotwoo.play I/MyService: onCreate
04-01 19:53:21.460 14704-14704/cn.hotwoo.play I/MyService: onBind
04-01 19:53:21.461 14704-14704/cn.hotwoo.play I/MyService: onServiceConnected
点击back键关闭activity或者调用Context.unbindService()方法后:
04-05 01:16:27.508 11427-11427/cn.hotwoo.play I/MyService: onUnbind
04-05 01:16:27.508 11427-11427/cn.hotwoo.play I/MyService: onDestroy
远程服务
调用者和service不在同一个进程中,service在单独的进程中的main线程,是一种垮进程通信方式。学习地址
绑定远程服务的步骤:
在服务的内部创建一个内部类,提供一个方法,可以间接调用服务的方法
把暴露的接口文件的扩展名改为.aidl文件 去掉访问修饰符
实现服务的onbind方法,继承Bander和实现aidl定义的接口,提供给外界可调用的方法
在activity 中绑定服务。bindService()
在服务成功绑定的时候会回调 onServiceConnected方法 传递一个 IBinder对象
aidl定义的接口.Stub.asInterface(binder) 调用接口里面的方法
IntentService
IntentService是Service的子类,比普通的Service增加了额外的功能。先看Service本身存在两个问题:
- Service不会专门启动一条单独的进程,Service与它所在应用位于同一个进程中;
- Service也不是专门一条新线程,因此不应该在Service中直接处理耗时的任务;
IntentService特征:
- 会创建独立的worker线程来处理所有的Intent请求;
- 会创建独立的worker线程来处理onHandleIntent()方法实现的代码,无需处理多线程问题;
- 所有请求处理完成后,IntentService会自动停止,无需调用stopSelf()方法停止Service;
为Service的onBind()提供默认实现,返回null; - 为Service的onStartCommand提供默认实现,将请求Intent添加到队列中;