两个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,每个 ...
随机推荐
- Spark API 之 combineByKey(一)
1 前言 combineByKey是使用Spark无法避免的一个方法,总会在有意或无意,直接或间接的调用到它.从它的字面上就可以知道,它有聚合的作用,对于这点不想做过多的解释,原因很简单, ...
- MVC知识进阶01
下面是在项目中有关于MVC的知识总结,有些地方若不对,请指出. 一:MVC的基本知识 1 名词解释 *惯例优先原则<约定大于配置>:是一种开发设计模式,说是在项目里面使用事先规定的命 ...
- Vue2.0流式渲染中文乱码问题
在参照vue2.0中文官方文档学习服务端渲染之流式渲染时,因为响应头默认编码类型为GBK,而文件为UFT-8类型,所以出现了中文乱码问题. 解决办法:设置响应头编码类型即可 response.setH ...
- 【转】 CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… PS:如需转载,请注明出处! 第1种效果: 代码如下: <div class="l ...
- 【C#】第3章学习要点(三)--常用类和结构的用法
分类:C#.VS2015 创建日期:2016-06-19 使用教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.DateTime结构和TimeSpan结构 DateT ...
- <if><else/></if> 语句
<li> <if condition="$nid eq 'partner'"> <a href="javascript:void(0);&q ...
- PHPUnit单元测试中类protected/private方法测试
这里首先有一个问题要考虑的是,这类方法是否要被测试? 理论上,这类方法都会被其它public类型的方法调用,只要对那些public的方法做充分的测试,就可以保证这些方法的可靠性,就没有必要再测了.好像 ...
- ahjesus可供C#使用的ip地理位置查询插件
http://dev.maxmind.com/geoip/geoip2/downloadable/#MaxMind_APIs 详情请阅读此链接及相关内容
- buffer和cache有什么本质区别
在free命令展示机器的内存消耗情况,会像这样展示
- 【背景建模】PbModel
PbModel是基于概率模型的背景差分算法,其基本思想是像素点会因光照变化.运动物体经过产生多种颜色值,但是一段时间内,像素点处于静止状态的时间会比处于运动状态的时间长.因而一段时间内,像素点某个颜色 ...