Android 两个Service的相互监视实现代码

所属分类: 软件编程 / Android 阅读数: 47
收藏 0 赞 0 分享

两个Service之间相互监视的实现

在实际开发中可能需要用到两个Service相互监视的情况,本示例就是实现此功能以作参考。

服务A:

public class ServiceA extends Service {


  private static final String TAG = ServiceA.class.getSimpleName();
  MyBinder mBinder;
  MyServiceConnection mServiceConnection;
  PendingIntent mPendingIntent;

  @Override
  public void onCreate() {
    super.onCreate();

    if(mBinder==null)
    {
      mBinder=new MyBinder();
    }
    mServiceConnection=new MyServiceConnection();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT);
    mPendingIntent=PendingIntent.getService(this,0,intent,0);
    Notification.Builder builder=new Notification.Builder(this);
    builder.setTicker("守护服务A启动中")
        .setContentText("我是来守护服务B的")
        .setContentTitle("守护服务A")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentIntent(mPendingIntent)
        .setWhen(System.currentTimeMillis());
    Notification notification=builder.build();

    startForeground(startId,notification);


    return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }

  public class MyBinder extends IBridgeInterface.Stub {

    @Override
    public String getName() throws RemoteException {
      return "name:"+TAG;
    }
  }

  class MyServiceConnection implements ServiceConnection {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
      String name=null;
      try {
        name= IBridgeInterface.Stub.asInterface(iBinder).getName();
      } catch (RemoteException e) {
        e.printStackTrace();
      }


      Toast.makeText(ServiceA.this,name+"连接成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
      Toast.makeText(ServiceA.this,TAG+"断开连接",Toast.LENGTH_SHORT).show();

      ServiceA.this.startService(new Intent(ServiceA.this,ServiceB.class));

      ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT);

    }
  }


}

服务B:

public class ServiceB extends Service {

  private static final String TAG = ServiceB.class.getSimpleName();
  private PendingIntent mPendingIntent;
  private MyBinder mBinder;
  private MyServiceConnection mServiceConnection;

  @Override
  public IBinder onBind(Intent intent) {
    return mBinder;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    if (mBinder == null) {
      mBinder = new MyBinder();
    }

    mServiceConnection = new MyServiceConnection();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
    mPendingIntent = PendingIntent.getService(this, 0, intent, 0);
    Notification.Builder builder = new Notification.Builder(this);

    builder.setTicker("守护服务B启动中")
        .setContentText("我是来守护服务A的")
        .setContentTitle("守护服务B")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentIntent(mPendingIntent)
        .setWhen(System.currentTimeMillis());
    Notification notification = builder.build();
    startForeground(startId, notification);

    return START_STICKY;
  }

  public class MyBinder extends IBridgeInterface.Stub {

    @Override
    public String getName() throws RemoteException {
      return "name:"+TAG;
    }
  }

  class MyServiceConnection implements ServiceConnection {

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
      String name=null;
      try {
        name=IBridgeInterface.Stub.asInterface(iBinder).getName();
      } catch (RemoteException e) {
        e.printStackTrace();
      }
      Toast.makeText(ServiceB.this, name + "连接成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
      Toast.makeText(ServiceB.this, TAG + "断开连接", Toast.LENGTH_SHORT).show();

      ServiceB.this.startService(new Intent(ServiceB.this, ServiceA.class));
      ServiceB.this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
    }
  }


}

IBridgeInterface.aidl

1 interface IBridgeInterface {
2   String getName();
3 }

界面:

public class MainActivity extends Activity {


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    startService(new Intent(this, ServiceA.class));
    startService(new Intent(this, ServiceB.class));
  }

}

AndroidManifest.xml

<service android: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 均依次传递。这适用于主动执行应该立即恢复的作业(例如下载文件)的服务。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多