涉及到服务通信,使用AIDL。

MyProcess.aidl:

package com.study.dn_process.inter;
interface MyProcess{String getProcessName();
}

LocalService:

package com.study.dn_process.service;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;import com.study.dn_process.R;
import com.study.dn_process.inter.MyProcess;@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class LocalService extends Service {private MyBinder binder;private MyServiceConnection conn;private PendingIntent pendingIntent;@Overridepublic IBinder onBind(Intent intent) {return binder;}@Overridepublic void onCreate() {super.onCreate();if (binder == null) {binder = new MyBinder();}conn = new MyServiceConnection();}@SuppressWarnings("deprecation")@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// LocalService.this.startService(new Intent(LocalService.this,// RemoteService.class));// 绑定远程服务this.bindService(new Intent(LocalService.this, RemoteService.class),conn, Context.BIND_IMPORTANT);Notification notification = new Notification(R.drawable.ic_launcher,"服务启动中", System.currentTimeMillis());pendingIntent = PendingIntent.getService(this, 0, intent, 0);notification.setLatestEventInfo(this, "服务", "---", pendingIntent);return START_STICKY;}class MyServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.e("info", "ServiceConnected");}@SuppressLint("InlinedApi")@Overridepublic void onServiceDisconnected(ComponentName name) {// 重新连接远程服务LocalService.this.startService(new Intent(LocalService.this,RemoteService.class));LocalService.this.bindService(new Intent(LocalService.this,RemoteService.class), conn, Context.BIND_IMPORTANT);}}class MyBinder extends MyProcess.Stub {@Overridepublic String getProcessName() throws RemoteException {return "LocalService";}}
}

RemoteService:

package com.study.dn_process.service;import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;import com.study.dn_process.R;
import com.study.dn_process.inter.MyProcess;@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class RemoteService extends Service {private MyBinder binder;private MyServiceConnection conn;private PendingIntent pendingIntent;@Overridepublic IBinder onBind(Intent intent) {return binder;}@Overridepublic void onCreate() {super.onCreate();if (binder == null) {binder = new MyBinder();}conn = new MyServiceConnection();}@SuppressWarnings("deprecation")@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// RemoteService.this.startService(new Intent(RemoteService.this,// LocalService.class));this.bindService(new Intent(RemoteService.this, LocalService.class),conn, Context.BIND_IMPORTANT);Notification notification = new Notification(R.drawable.ic_launcher,"服务启动中", System.currentTimeMillis());pendingIntent = PendingIntent.getService(this, 0, intent, 0);notification.setLatestEventInfo(this, "服务", "---", pendingIntent);return START_STICKY;}class MyServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.e("info", "ServiceConnected");}@SuppressLint("InlinedApi")@Overridepublic void onServiceDisconnected(ComponentName name) {RemoteService.this.startService(new Intent(RemoteService.this,LocalService.class));RemoteService.this.bindService(new Intent(RemoteService.this,LocalService.class), conn, Context.BIND_IMPORTANT);}}class MyBinder extends MyProcess.Stub {@Overridepublic String getProcessName() throws RemoteException {return "RemoteService";}}
}

MainActivity:

package com.study.dn_process;import com.study.dn_process.service.LocalService;
import com.study.dn_process.service.RemoteService;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.startService(new Intent(this, LocalService.class));this.startService(new Intent(this, RemoteService.class));}}

tip:清单文件需要注册!

双守护进程保护程序运行相关推荐

  1. linux php 守护进程,PHP程序员玩转Linux系列 使用supervisor实现守护进程

    PHP程序员玩转Linux系列文章: 首先遇到的问题是,部署nodejs的博客程序时,我把执行nodejs的命令放到后台,使用加&和nohup命令 如:nodejs index.js & ...

  2. java 守护进程 linux_Java实现Linux下服务器程序的双守护进程

    一.简介 现在的服务器端程序很多都是基于Java开发,针对于Java开发的Socket程序,这样的服务器端上线后出现问题需要手动重启,万一大半夜的挂了,还是特别麻烦的. 大多数的解决方法是使用其他进程 ...

  3. Linux c 监控进程状态,linux进程监控和守护进程的程序_linux程序一实现守护进程,用于监测程序二的启动状态-C代码类资源...

    linux实现对开启进程的监控,1,从配置文件中获得要开启的进程名 2,放入结构体数组中3,用fork加exel启动进程,4,检测proc下进程是否在运行,没有运行,则开启此进程5, struct p ...

  4. Python 多线程、守护进程、同时运行最大线程数、锁、线程阻塞(线程暂停和继续)

    python 多线程的使用笔记 1.多线程的基本用法 (1)简单任务多线程的开启方式 from threading import Thread import timedef target(name, ...

  5. python3 编写守护进程程序思路

    1. fork子进程,父进程退出 通常,我们执行服务端程序的时候都会通过终端连接到服务器,成功连接后会加载shell环境,终端和shell都是进程,shell进程是终端进程的子进程,通过ps命令可以很 ...

  6. 程序、进程和守护进程的区别

    程序.进程和守护进程的区别 程序是软件代码的集合 进程是运行着的程序 守护进程是一直运行着的程序 程序也称为软件,是指一组指示计算机或其他具有信息处理能力装置每一步动作的指令,是由某种程序设计语言编写 ...

  7. linux守护进程以及如何编写守护进程程序

    守护进程 理论 「守护进程」是 Linux 的一种长期运行的后台服务进程,也有人称它为「精灵进程」.我们常见的 httpd.named.sshd 等服务都是以守护进程 Daemon 方式运行的,通常服 ...

  8. java程序员的大数据之路(12):Hadoop的守护进程

    关键属性 Hadoop守护进程的关键属性大多标记为final,使作业的配置无法覆盖. 典型的core-site.xml配置文件 <?xml version="1.0"> ...

  9. java守护锁_Java 对象锁-synchronized()与线程的状态与生命周期与守护进程

    synchronized(someObject){ //对象锁 } 一.对象锁 someObject 的使用说明: 1.对象锁的返还. 当synchronize()语句执行完成. 当synchroni ...

最新文章

  1. 数字孪生等前沿技术,将如何解码未来交通?
  2. ubuntu常见错误–Could not get lock /var/lib/dpkg/lock解决
  3. python职业规划书_Python学习过后,职业规划怎样规划?
  4. Android 自定义Switch,仿微信开关键Switch
  5. [转]《帮我买个单》
  6. java求阶乘1-20_java求1+2!+3!+...+20!的和,java1到20的阶乘
  7. java 一元二次方程_java求解一元二次方程
  8. 分位数回归及Stata实现
  9. php web helloworld,webim_server.php
  10. Oracle 一致性读
  11. python百度识别花草_用python代码实现调用百度的免费植物识别接口
  12. 修复谷歌浏览器翻译功能(win10)
  13. 供应链金融服务平台系统开发-成熟、稳定、节本、增效,一站式信息交易管理平台
  14. 【1+X Web前端等级考证 】 | Web前端开发中级理论 (附答案)
  15. 没有空闲时间时该如何做一个业余项目?
  16. python打印a-z的字母
  17. LiveUpdate Adminstrator配置
  18. 在HTML中打开windows计算器
  19. 浪潮服务器NP5720M4安装vm虚拟化
  20. Exchange(交换机)的作用以及类型

热门文章

  1. 5G网络(接入网+承载网+核心网)
  2. 如何测试承载网——TFN TT60 综合网络测试仪
  3. OpenCV利用Haar Cascades进行人脸检测
  4. linux网卡驱动离线安装_在linux下安装网卡驱动的方法
  5. STM32 USB虚拟串口收发任意长度字节例程
  6. OpenStack Liberty版本容器模块Magnum的极速体验
  7. android中底部弹窗,Android实现从底部弹出的Dialog示例(一)
  8. 易点易动固定资产管理系统让行政和IT人员快速盘点固定资产
  9. ERROR: The Compose file ‘./docker/docker-compose-test-net.yaml‘ is invalid because: networks.test va
  10. wp7各种音乐播放器下载大全