两个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
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之间相互监视的实现的更多相关文章
- vue-router 两个子路由之间相互跳转时出错
patient页面跳转到apply页面后,再点击patient页面后无法跳回 解决方法:使用`${path + path1}` 来自为知笔记(Wiz)
- Visual Studio 中两个窗体(WinForm)之间相互传值的方法
编写WinowsForm应用程序时,实现两个窗体之间相互传递值的方法其实很简单.以下用一个例子说明:在名为FormMain主窗体运行过程中利用名为FormInfo窗体,获取用户输入信息,并将这些信息返 ...
- Android消息机制之实现两个不同线程之间相互传递数据相互调用
目的:实现两个不同线程之间相互传递数据相互调用方法. 线程一中定义mainHandler 并定义一个方法mainDecode 线程二中定义twoHandler 并定义一个方法twoEncode 实现当 ...
- 通过AIDL在两个APP之间Service通信
一.项目介绍 [知识准备] ①Android Interface definition language(aidl,android接口定义语言),其目的实现跨进程的调用.进程是程序在os中执行的载体, ...
- 吧,其实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 ...
- 【原】linux两台服务器之间免密登录方法
搭建集群机器192.168.0.100和192.168.0.200里,需要两台机器中间相互拷贝文件: 方式一:下载192.168.0.100机器文件到本地,再将本地文件拷贝到B机器 方式二:192.1 ...
- SHAREPOINT 2013 列表之间相互关联
修改内容 1.增加列表设置,隐藏Aid字段操作 SharePoint 列表之间相互关联 例如两张列表之间的父子关系. 思路如下: 列表中新增列表项后会有一个唯一的ID,我们获取到该ID赋予子表即可将两 ...
- js 获取小数点位数方法及 字符串与数字之间相互转换方法
1.获取小数点位数方法 a. 使用 js 中 subsrting,indexOf,parseFloat三个函数,代码如下: var s = "22.127456" ;//s 为 字 ...
- Service 之间如何通信?- 每天5分钟玩转 Docker 容器技术(101)
微服务架构的应用由若干 service 组成.比如有运行 httpd 的 web 前端,有提供缓存的 memcached,有存放数据的 mysql,每一层都是 swarm 的一个 service,每个 ...
随机推荐
- js基础篇——call/apply、arguments、undefined/null
a.call和apply方法详解 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象 ...
- ASP.NET 文件下载
using System; using System.Web; using System.IO; public partial class _Default : System.Web.UI.Page ...
- IOS从视频中获取截图
从视频中获取截图: NSString *movpath =[[NSBundle mainBundle] pathForResource:@”iosxcode4″ ofType:@”mov”]; mpv ...
- 微信小程序官方文档错误整理
大致看了一遍微信小程序文档,发现有几处微小的错误,但瑕不掩瑜.记录下,以后发现了还会继续在此添加.如果有记录不对的,请及时指出错误. 1.视图层->WXSS->尺寸单位 明显错误,应该为 ...
- 数学 - Whu 1603 - Minimum Sum
Minimum Sum Problem's Link ------------------------------------------------------------------------- ...
- javascript设计模式实践之策略模式--输入验证
策略模式中的策略就是一种算法或者业务规则,将这些策略作为函数进行封装,并向外提供统一的调用执行. 先定义一个简单的输入表单: <!DOCTYPE html> <html> &l ...
- Rebalance Customer Balances Utility的使用
用户不管是打开A/R Posted Transactions Detail还是A/R Posted Transactions Summary 窗口,均显示如下一个警示: 打开Currency Code ...
- Entity Framework 实体框架的形成之旅--利用Unity对象依赖注入优化实体框架(2)
在本系列的第一篇随笔<Entity Framework 实体框架的形成之旅--基于泛型的仓储模式的实体框架(1)>中介绍了Entity Framework 实体框架的一些基础知识,以及构建 ...
- HTTPS能有效保护用户隐私
HTTPS就等于HTTP加上TLS(SSL),HTTPS协议的目标主要有三个: http://hovertree.com/menu/webfront/ 数据保密性.保证内容在传输过程中不会被第三方查看 ...
- Extjs 回车查询
listeners: { afterRender: function (thisForm, options) { this.keyNav = Ext.create('Ext.util.KeyNav', ...