今天聊一聊Service。
Service是Android里的计算型组件,它是Android中四大组件之一。提到Service,不得不说的就是它的两种启动方式了,不着急,还是一步一步来。
1.概念:
Service是Android程序中四大基础组件之一,它和Activity一样都是Context的子类,只不过它没有UI界面,是在后台运行的组件。
2.生命周期:
Service对象不能自己启动,需要通过某个Activity、Service或者其他Context对象来启动。启动的方法有两种,Context.startService和Context.bindService()。两种方式的生命周期是不同的,具体如下所示。
Context.startService方式的生命周期:
启动时,startService –> onCreate() –> onStart()
停止时,stopService –> onDestroy()
Context.bindService方式的生命周期:
解绑定时,unbindService –>onUnbind() –> onDestory()
3.Service与Thread的区别:
最重要的区别就是,Service也是运行在主线程的,不可以在Service中执行耗时操作,如果需要下载等耗时操作,仍需要在Service中开启新线程去执行。
4.重头戏:启动的两种方式,我觉得用代码会更直观的看出两者区别:
(1)直接启动:StartService:
(A)定义一个类继承Service
(B)在AndroidManifest.xml文件中配置改Service
(C)使用Context的StartService(Intent)启动该Service;
(D)不再使用时,直接调用StopService(Intent);
public class ServiceTestStart extends Service { /** * 普通开启方式 */ @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(this,"服务被创建",Toast.LENGTH_SHORT).show(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this,"服务开启",Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.e("dddddddddddddddddddd","服务被销毁"); Toast.makeText(this,"服务被销毁",Toast.LENGTH_SHORT).show(); } }
在MainActivity中开启:
case R.id.btn_start: startService(new Intent(MainActivity.this, ServiceTestStart.class)); break; case R.id.btn_stop: stopService(new Intent(MainActivity.this, ServiceTestStart.class)); break;
(2)绑定启动:
(A)创建BindService服务端,继承Service并在类中创建一个实现IBinder接口的实例对象,并提供公共方法给客户端调用;
(B)从onBind()回调方法返回此Binder实例
(C)在客户端中,从onServiceConnect()回调方法接收Binder,并使用提供的方法调用绑定服务;
public class ServiceTestBind extends Service { A a=new A(); @Nullable @Override public IBinder onBind(Intent intent) { return a; } @Override public void onCreate() { Toast.makeText(this,"服务被绑定创建",Toast.LENGTH_SHORT).show(); super.onCreate(); } @Override public void onDestroy() { Toast.makeText(this,"服务被销毁",Toast.LENGTH_SHORT).show(); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this,"服务启动",Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent, flags, startId); } public class A extends Binder{ public ServiceTestBind getServiceTest(){ return ServiceTestBind.this; } } public void getString(){ Log.e("服务的方法被调用了","哈哈哈,我被调用了"); } }
在MainActivity中实现onServiceConnection接口,实现其两个方法,通过IBinder调用Service中的公共方法
class Connection implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { ServiceTestBind.A a= (ServiceTestBind.A) service; ServiceTestBind serviceTest = a.getServiceTest(); serviceTest.getString(); } @Override public void onServiceDisconnected(ComponentName name) { Log.e("dsadasdadadad","服务断开连接"); } }
case R.id.btn_bind: Intent intent=new Intent(MainActivity.this, ServiceTestBind.class); bindService(intent,conn, Service.BIND_AUTO_CREATE); break; case R.id.btn_unbind: unbindService(conn); break;至于Binder,在后面的博客中会专有一篇去详解的。