网友通过本文主要向大家介绍了实现service,service的实现方法,web service实现平台,service,service unavailable等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
两个Service之间相互监视的实现,两个service监视
在实际开发中可能需要用到两个Service相互监视的情况,本示例就是实现此功能以作参考。
服务A:
1 public class ServiceA extends Service { 2 3 4 private static final String TAG = ServiceA.class.getSimpleName(); 5 MyBinder mBinder; 6 MyServiceConnection mServiceConnection; 7 PendingIntent mPendingIntent; 8 9 @Override 10 public void onCreate() { 11 super.onCreate(); 12 13 if(mBinder==null) 14 { 15 mBinder=new MyBinder(); 16 } 17 mServiceConnection=new MyServiceConnection(); 18 } 19 20 @Override 21 public int onStartCommand(Intent intent, int flags, int startId) { 22 ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT); 23 mPendingIntent=PendingIntent.getService(this,0,intent,0); 24 Notification.Builder builder=new Notification.Builder(this); 25 builder.setTicker("守护服务A启动中") 26 .setContentText("我是来守护服务B的") 27 .setContentTitle("守护服务A") 28 .setSmallIcon(R.mipmap.ic_launcher) 29 .setContentIntent(mPendingIntent) 30 .setWhen(System.currentTimeMillis()); 31 Notification notification=builder.build(); 32 33 startForeground(startId,notification); 34 35 36 return START_STICKY; 37 } 38 39 @Override 40 public IBinder onBind(Intent intent) { 41 return mBinder; 42 } 43 44 public class MyBinder extends IBridgeInterface.Stub { 45 46 @Override 47 public String getName() throws RemoteException { 48 return "name:"+TAG; 49 } 50 } 51 52 class MyServiceConnection implements ServiceConnection { 53 54 @Override 55 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 56 String name=null; 57 try { 58 name= IBridgeInterface.Stub.asInterface(iBinder).getName(); 59 } catch (RemoteException e) { 60 e.printStackTrace(); 61 } 62 63 64 Toast.makeText(ServiceA.this,name+"连接成功",Toast.LENGTH_SHORT).show(); 65 } 66 67 @Override 68 public void onServiceDisconnected(ComponentName componentName) { 69 Toast.makeText(ServiceA.this,TAG+"断开连接",Toast.LENGTH_SHORT).show(); 70 71 ServiceA.this.startService(new Intent(ServiceA.this,ServiceB.class)); 72 73 ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT); 74 75 } 76 } 77 78 79 }
服务B:
1 public class ServiceB extends Service { 2 3 private static final String TAG = ServiceB.class.getSimpleName(); 4 private PendingIntent mPendingIntent; 5 private MyBinder mBinder; 6 private MyServiceConnection mServiceConnection; 7 8 @Override 9 public IBinder onBind(Intent intent) { 10 return mBinder; 11 } 12 13 @Override 14 public void onCreate() { 15 super.onCreate(); 16 if (mBinder == null) { 17 mBinder = new MyBinder(); 18 } 19 20 mServiceConnection = new MyServiceConnection(); 21 } 22 23 @Override 24 public int onStartCommand(Intent intent, int flags, int startId) { 25 this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT); 26 mPendingIntent = PendingIntent.getService(this, 0, intent, 0); 27 Notification.Builder builder = new Notification.Builder(this); 28 29 builder.setTicker("守护服务B启动中") 30 .setContentText("我是来守护服务A的") 31 .setContentTitle("守护服务B") 32 .setSmallIcon(R.mipmap.ic_launcher) 33 .setContentIntent(mPendingIntent) 34 .setWhen(System.currentTimeMillis()); 35 Notification notification = builder.build(); 36 startForeground(startId, notification); 37 38 retur