Android 两个Service的相互监视实现代码
两个Service之间相互监视的实现
在实际开发中可能需要用到两个Service相互监视的情况,本示例就是实现此功能以作参考。
服务A:
publicclassServiceAextendsService{ privatestaticfinalStringTAG=ServiceA.class.getSimpleName(); MyBindermBinder; MyServiceConnectionmServiceConnection; PendingIntentmPendingIntent; @Override publicvoidonCreate(){ super.onCreate(); if(mBinder==null) { mBinder=newMyBinder(); } mServiceConnection=newMyServiceConnection(); } @Override publicintonStartCommand(Intentintent,intflags,intstartId){ ServiceA.this.bindService(newIntent(ServiceA.this,ServiceB.class),mServiceConnection,Context.BIND_IMPORTANT); mPendingIntent=PendingIntent.getService(this,0,intent,0); Notification.Builderbuilder=newNotification.Builder(this); builder.setTicker("守护服务A启动中") .setContentText("我是来守护服务B的") .setContentTitle("守护服务A") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(mPendingIntent) .setWhen(System.currentTimeMillis()); Notificationnotification=builder.build(); startForeground(startId,notification); returnSTART_STICKY; } @Override publicIBinderonBind(Intentintent){ returnmBinder; } publicclassMyBinderextendsIBridgeInterface.Stub{ @Override publicStringgetName()throwsRemoteException{ return"name:"+TAG; } } classMyServiceConnectionimplementsServiceConnection{ @Override publicvoidonServiceConnected(ComponentNamecomponentName,IBinderiBinder){ Stringname=null; try{ name=IBridgeInterface.Stub.asInterface(iBinder).getName(); }catch(RemoteExceptione){ e.printStackTrace(); } Toast.makeText(ServiceA.this,name+"连接成功",Toast.LENGTH_SHORT).show(); } @Override publicvoidonServiceDisconnected(ComponentNamecomponentName){ Toast.makeText(ServiceA.this,TAG+"断开连接",Toast.LENGTH_SHORT).show(); ServiceA.this.startService(newIntent(ServiceA.this,ServiceB.class)); ServiceA.this.bindService(newIntent(ServiceA.this,ServiceB.class),mServiceConnection,Context.BIND_IMPORTANT); } } }
服务B:
publicclassServiceBextendsService{ privatestaticfinalStringTAG=ServiceB.class.getSimpleName(); privatePendingIntentmPendingIntent; privateMyBindermBinder; privateMyServiceConnectionmServiceConnection; @Override publicIBinderonBind(Intentintent){ returnmBinder; } @Override publicvoidonCreate(){ super.onCreate(); if(mBinder==null){ mBinder=newMyBinder(); } mServiceConnection=newMyServiceConnection(); } @Override publicintonStartCommand(Intentintent,intflags,intstartId){ this.bindService(newIntent(ServiceB.this,ServiceA.class),mServiceConnection,Context.BIND_IMPORTANT); mPendingIntent=PendingIntent.getService(this,0,intent,0); Notification.Builderbuilder=newNotification.Builder(this); builder.setTicker("守护服务B启动中") .setContentText("我是来守护服务A的") .setContentTitle("守护服务B") .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(mPendingIntent) .setWhen(System.currentTimeMillis()); Notificationnotification=builder.build(); startForeground(startId,notification); returnSTART_STICKY; } publicclassMyBinderextendsIBridgeInterface.Stub{ @Override publicStringgetName()throwsRemoteException{ return"name:"+TAG; } } classMyServiceConnectionimplementsServiceConnection{ @Override publicvoidonServiceConnected(ComponentNamecomponentName,IBinderiBinder){ Stringname=null; try{ name=IBridgeInterface.Stub.asInterface(iBinder).getName(); }catch(RemoteExceptione){ e.printStackTrace(); } Toast.makeText(ServiceB.this,name+"连接成功",Toast.LENGTH_SHORT).show(); } @Override publicvoidonServiceDisconnected(ComponentNamecomponentName){ Toast.makeText(ServiceB.this,TAG+"断开连接",Toast.LENGTH_SHORT).show(); ServiceB.this.startService(newIntent(ServiceB.this,ServiceA.class)); ServiceB.this.bindService(newIntent(ServiceB.this,ServiceA.class),mServiceConnection,Context.BIND_IMPORTANT); } } }
IBridgeInterface.aidl
1interfaceIBridgeInterface{ 2StringgetName(); 3}
界面:
publicclassMainActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(newIntent(this,ServiceA.class)); startService(newIntent(this,ServiceB.class)); } }
AndroidManifest.xml
<serviceandroid:name=".services.ServiceA"/> <service android:name=".services.ServiceB" android:process=":remote"/>
由于涉及到跨进程,onServiceConnected()方法中使用
IBridgeInterface.Stub.asInterface(iBinder).getName();
而不能直接类型转换
((ServiceA.MyBinder)iBinder).getName();
onStartCommand
onStartCommand()方法必须返回整型数。整型数是一个值,用于描述系统应该如何在服务终止的情况下继续运行服务。
返回的值必须是以下常量之一:
START_NOT_STICKY
如果系统在onStartCommand()返回后终止服务,则除非有挂起Intent要传递,否则系统不会重建服务。
START_STICKY
如果系统在onStartCommand()返回后终止服务,则会重建服务并调用onStartCommand(),但绝对不会重新传递最后一个Intent。相反,除非有挂起Intent要启动服务(在这种情况下,将传递这些Intent),否则系统会通过空Intent调用onStartCommand()。这适用于不执行命令、但无限期运行并等待作业的媒体播放器(或类似服务)。
START_REDELIVER_INTENT
如果系统在onStartCommand()返回后终止服务,则会重建服务,并通过传递给服务的最后一个Intent调用onStartCommand()。任何挂起Intent均依次传递。这适用于主动执行应该立即恢复的作业(例如下载文件)的服务。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!