在实际开发中可能需要用到两个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

 interface IBridgeInterface {
String getName();
}

界面:

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

两个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. jQuery 简介和安装

    什么是jQuery jQuery是一个轻量级的"写的少,做的多"的JavaScript库. jQuery库包含以下功能: HTML 元素选取 HTML 元素操作 CSS 操作 HT ...

  2. 转 Windows+VS2013爆详细Caffe编译安装教程

    1. 安装cuda Cuda是英伟达推出的GPU加速运算平台 我这里安装的是cuda7.5,已经安装过的忽略,还没有安装过的这里有安装教程.windows下面安装还是非常简单的. 点击打开链接    ...

  3. shell脚本二

    在shell脚本一 中,我讨论了shell脚本的语法规范,shell脚本的变量,以及shell脚本的测试语句. 仅仅懂得这些只能写简单的脚本,在简单的脚本中各条语句按顺序执行,从而实现自动化的管理,顺 ...

  4. UI自动化测试框架(项目实战)python、Selenium(日志、邮件、pageobject)

    其实百度UI自动化测试框架,会出来很多相关的信息,不过就没有找到纯项目的,无法拿来使用的:所以我最近就写了一个简单,不过可以拿来在真正项目中可以使用的测试框架. 项目的地址:https://githu ...

  5. MVC学习之前必须掌握的c#基础知识

    一.类自动属性 public class Person { //自动属性 public string Name { get; set; } private int _age; public int a ...

  6. JAVA - IDEA快捷键(精简版)

    快捷键 功能 Ctrl + Alt + V 对应eclipse ctrl + l + 2 自动补全 Ctrl + Alt + L 对应eclipse ctrl + shift + o 代码格式化 Ct ...

  7. AlertDialog.Builder弹出对话框

    在Android中,弹出对话框使用AlertDialog.Builder方法. new AlertDialog.Builder(MainActivity.this).setTitle("本机 ...

  8. Scalaz(35)- Free :运算-Trampoline,say NO to StackOverflowError

    在前面几次讨论中我们介绍了Free是个产生Monad的最基本结构.它的原理是把一段程序(AST)一连串的运算指令(ADT)转化成数据结构存放在内存里,这个过程是个独立的功能描述过程.然后另一个独立运算 ...

  9. Java中查找文件并且打印输出指定文件下面的子目录

    package com.immoc; import java.io.File; import java.io.IOException; public class FileUtile { //列出fil ...

  10. [javascript] 判断 iframe 是否加载完成

    from http://www.planabc.net/2009/09/22/iframe_onload/ var iframe = document.createElement("ifra ...