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

  1. 服务A
  1. public class ServiceA extends Service {
  2.  
  3. private static final String TAG = ServiceA.class.getSimpleName();
  4. MyBinder mBinder;
  5. MyServiceConnection mServiceConnection;
  6. PendingIntent mPendingIntent;
  7.  
  8. @Override
  9. public void onCreate() {
  10. super.onCreate();
  11.  
  12. if(mBinder==null)
  13. {
  14. mBinder=new MyBinder();
  15. }
  16. mServiceConnection=new MyServiceConnection();
  17. }
  18.  
  19. @Override
  20. public int onStartCommand(Intent intent, int flags, int startId) {
  21. ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT);
  22. mPendingIntent=PendingIntent.getService(this,0,intent,0);
  23. Notification.Builder builder=new Notification.Builder(this);
  24. builder.setTicker("守护服务A启动中")
  25. .setContentText("我是来守护服务B的")
  26. .setContentTitle("守护服务A")
  27. .setSmallIcon(R.mipmap.ic_launcher)
  28. .setContentIntent(mPendingIntent)
  29. .setWhen(System.currentTimeMillis());
  30. Notification notification=builder.build();
  31.  
  32. startForeground(startId,notification);
  33.  
  34. return START_STICKY;
  35. }
  36.  
  37. @Override
  38. public IBinder onBind(Intent intent) {
  39. return mBinder;
  40. }
  41.  
  42. public class MyBinder extends IBridgeInterface.Stub {
  43.  
  44. @Override
  45. public String getName() throws RemoteException {
  46. return "name:"+TAG;
  47. }
  48. }
  49.  
  50. class MyServiceConnection implements ServiceConnection {
  51.  
  52. @Override
  53. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  54. String name=null;
  55. try {
  56. name= IBridgeInterface.Stub.asInterface(iBinder).getName();
  57. } catch (RemoteException e) {
  58. e.printStackTrace();
  59. }
  60.  
  61. Toast.makeText(ServiceA.this,name+"连接成功",Toast.LENGTH_SHORT).show();
  62. }
  63.  
  64. @Override
  65. public void onServiceDisconnected(ComponentName componentName) {
  66. Toast.makeText(ServiceA.this,TAG+"断开连接",Toast.LENGTH_SHORT).show();
  67.  
  68. ServiceA.this.startService(new Intent(ServiceA.this,ServiceB.class));
  69.  
  70. ServiceA.this.bindService(new Intent(ServiceA.this,ServiceB.class),mServiceConnection, Context.BIND_IMPORTANT);
  71.  
  72. }
  73. }
  74.  
  75. }

服务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. return START_STICKY;
  39. }
  40.  
  41. public class MyBinder extends IBridgeInterface.Stub {
  42.  
  43. @Override
  44. public String getName() throws RemoteException {
  45. return "name:"+TAG;
  46. }
  47. }
  48.  
  49. class MyServiceConnection implements ServiceConnection {
  50.  
  51. @Override
  52. public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
  53. String name=null;
  54. try {
  55. name=IBridgeInterface.Stub.asInterface(iBinder).getName();
  56. } catch (RemoteException e) {
  57. e.printStackTrace();
  58. }
  59. Toast.makeText(ServiceB.this, name + "连接成功", Toast.LENGTH_SHORT).show();
  60. }
  61.  
  62. @Override
  63. public void onServiceDisconnected(ComponentName componentName) {
  64. Toast.makeText(ServiceB.this, TAG + "断开连接", Toast.LENGTH_SHORT).show();
  65.  
  66. ServiceB.this.startService(new Intent(ServiceB.this, ServiceA.class));
  67. ServiceB.this.bindService(new Intent(ServiceB.this, ServiceA.class), mServiceConnection, Context.BIND_IMPORTANT);
  68. }
  69. }
  70.  
  71. }

IBridgeInterface.aidl

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

界面:

  1. public class MainActivity extends Activity {
  2.  
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7.  
  8. startService(new Intent(this, ServiceA.class));
  9. startService(new Intent(this, ServiceB.class));
  10. }
  11.  
  12. }

AndroidManifest.xml

  1. <service android:name=".services.ServiceA" />
  2. <service
  3. android:name=".services.ServiceB"
  4. android:process=":remote" />

由于涉及到跨进程,onServiceConnected() 方法中使用

  1. IBridgeInterface.Stub.asInterface(iBinder).getName();

而不能直接类型转换

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

两个Service之间相互监视的实现的更多相关文章

  1. vue-router 两个子路由之间相互跳转时出错

    patient页面跳转到apply页面后,再点击patient页面后无法跳回 解决方法:使用`${path + path1}` 来自为知笔记(Wiz)

  2. Visual Studio 中两个窗体(WinForm)之间相互传值的方法

    编写WinowsForm应用程序时,实现两个窗体之间相互传递值的方法其实很简单.以下用一个例子说明:在名为FormMain主窗体运行过程中利用名为FormInfo窗体,获取用户输入信息,并将这些信息返 ...

  3. Android消息机制之实现两个不同线程之间相互传递数据相互调用

    目的:实现两个不同线程之间相互传递数据相互调用方法. 线程一中定义mainHandler 并定义一个方法mainDecode 线程二中定义twoHandler 并定义一个方法twoEncode 实现当 ...

  4. 通过AIDL在两个APP之间Service通信

    一.项目介绍 [知识准备] ①Android Interface definition language(aidl,android接口定义语言),其目的实现跨进程的调用.进程是程序在os中执行的载体, ...

  5. 吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个javabean之间的相互拷贝,自己写的就当是研究咯---https://www.cnblogs.com/NieXiaoHui/p/7150928.html

    吧,其实spring自带的BeanUtils就有这样的功能,引入spring-beans和spring-core之后,就有BeanUtils.copyProperties(a, b);可以实现两个ja ...

  6. 【原】linux两台服务器之间免密登录方法

    搭建集群机器192.168.0.100和192.168.0.200里,需要两台机器中间相互拷贝文件: 方式一:下载192.168.0.100机器文件到本地,再将本地文件拷贝到B机器 方式二:192.1 ...

  7. SHAREPOINT 2013 列表之间相互关联

    修改内容 1.增加列表设置,隐藏Aid字段操作 SharePoint 列表之间相互关联 例如两张列表之间的父子关系. 思路如下: 列表中新增列表项后会有一个唯一的ID,我们获取到该ID赋予子表即可将两 ...

  8. js 获取小数点位数方法及 字符串与数字之间相互转换方法

    1.获取小数点位数方法 a. 使用 js 中 subsrting,indexOf,parseFloat三个函数,代码如下: var s = "22.127456" ;//s 为 字 ...

  9. Service 之间如何通信?- 每天5分钟玩转 Docker 容器技术(101)

    微服务架构的应用由若干 service 组成.比如有运行 httpd 的 web 前端,有提供缓存的 memcached,有存放数据的 mysql,每一层都是 swarm 的一个 service,每个 ...

随机推荐

  1. Spark API 之 combineByKey(一)

    1       前言 combineByKey是使用Spark无法避免的一个方法,总会在有意或无意,直接或间接的调用到它.从它的字面上就可以知道,它有聚合的作用,对于这点不想做过多的解释,原因很简单, ...

  2. MVC知识进阶01

    下面是在项目中有关于MVC的知识总结,有些地方若不对,请指出. 一:MVC的基本知识 1 名词解释    *惯例优先原则<约定大于配置>:是一种开发设计模式,说是在项目里面使用事先规定的命 ...

  3. Vue2.0流式渲染中文乱码问题

    在参照vue2.0中文官方文档学习服务端渲染之流式渲染时,因为响应头默认编码类型为GBK,而文件为UFT-8类型,所以出现了中文乱码问题. 解决办法:设置响应头编码类型即可 response.setH ...

  4. 【转】 CSS3实现10种Loading效果

    昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… PS:如需转载,请注明出处! 第1种效果: 代码如下: <div class="l ...

  5. 【C#】第3章学习要点(三)--常用类和结构的用法

    分类:C#.VS2015 创建日期:2016-06-19 使用教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.DateTime结构和TimeSpan结构 DateT ...

  6. <if><else/></if> 语句

    <li> <if condition="$nid eq 'partner'"> <a href="javascript:void(0);&q ...

  7. PHPUnit单元测试中类protected/private方法测试

    这里首先有一个问题要考虑的是,这类方法是否要被测试? 理论上,这类方法都会被其它public类型的方法调用,只要对那些public的方法做充分的测试,就可以保证这些方法的可靠性,就没有必要再测了.好像 ...

  8. ahjesus可供C#使用的ip地理位置查询插件

    http://dev.maxmind.com/geoip/geoip2/downloadable/#MaxMind_APIs 详情请阅读此链接及相关内容

  9. buffer和cache有什么本质区别

    在free命令展示机器的内存消耗情况,会像这样展示

  10. 【背景建模】PbModel

    PbModel是基于概率模型的背景差分算法,其基本思想是像素点会因光照变化.运动物体经过产生多种颜色值,但是一段时间内,像素点处于静止状态的时间会比处于运动状态的时间长.因而一段时间内,像素点某个颜色 ...