两个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,每个 ...
随机推荐
- SQL Server安全(3/11):主体和安全对象(Principals and Securables)
在保密你的服务器和数据,防备当前复杂的攻击,SQL Server有你需要的一切.但在你能有效使用这些安全功能前,你需要理解你面对的威胁和一些基本的安全概念.这篇文章提供了基础,因此你可以对SQL Se ...
- 基于HT for Web矢量实现2D叶轮旋转
之前在拓扑上的应用都是些静态的图元,今天我们将在拓扑上设计一个会动的图元——叶轮旋转. 我们先来看下这个叶轮模型长什么样 从模型上看,这个叶轮模型有三个叶片,每一个叶片都是不规则图形,显然无法用上我们 ...
- 找到多个与名为“Login”的控制器匹配的类型
添加了mvc区域以后,可能出现以下错误... 找到多个与名为“Login”的控制器匹配的类型.如果为此请求(“{controller}/{action}/{id}”)提供服务的路由在搜索匹配此请求的控 ...
- Winform开发框架中实现多种数据库类型切换以及分拆数据库的支持
在很多应用系统里面,虽然一般采用一种数据库运行,但是由于各种情况的需要,可能业务系统会部署在不同类型的数据库上,如果开发的系统能够很方便支持多种数据库的切换,那可以为我们减少很多烦恼,同时提高系统的适 ...
- C#调用Java方法(详细实例)
C#可以直接引用C++的DLL和转换JAVA写好的程序.最近由于工作原因接触这方面比较多,根据实际需求,我们通过一个具体例子把一个JAVA方法转换成可以由C#直接调用的DLL C#调用c++ C#调用 ...
- 创建Oracle数据库
[root@localhost ~]# su - oracle [oracle@localhost ~]$ sqlplus /nolog SQL> conn /as sysdba; SQL> ...
- Ado.net 三[SQL注入,DataAdapter,sqlParameter,DataSet]
1.SQL注入:SQL注入攻击是web应用程序的一种安全漏洞,可以将不安全的数据提交给运用程序,使应用程序在服务器上执行不安全的sql命令.使用该攻击可以轻松的登录运用程序. 例如:该管理员账号密码为 ...
- 在Win Server 2012中安装.NET Framework 3.5的问题
在Windows Server 2012 上安装 SQL Server 2012 时,提示 启用 Windows 功能 NetFx3 时出错,错误代码:-2146498298.请尝试从 Windows ...
- font-family常见字体
font-family:"Times New Roman",Georgia,Serif font-family:Arial,Verdana,Sans-serif font-fami ...
- FreeBSD应该装gnome3做桌面
目前freebsd pkg包管理体系的repo源多了一些,速度快了很多. 仓库中目前的版本为3.14,安装gnome3很简单. pkg install xorg gnome3 echo "e ...