热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android保活进程总结--双进程保活策略

进程的保活,在很多资讯类的App和即时通讯App的用处很大,奈何谷歌的推送服务在国内是被阉割了!据说是在8.0(奥利奥)相关政府机构已经将开放这项功能提上了日程,嗯,没错8.0,预计再过三五年就

进程的保活,在很多资讯类的App和即时通讯App的用处很大,奈何谷歌的推送服务在国内是被阉割了!据说是在8.0(奥利奥)相关政府机构已经将开放这项功能提上了日程,嗯,没错8.0,预计再过三五年就可以像苹果那样自由自在的推送了!但是一点不鸡冻,不知道我这秃头的码农还能不能挨到,哈哈!

先看看效果图:

这里写图片描述

这张图片的主要操作是杀死后台所有的应用进程,之后在状态栏上面弹出被拉起来的程!

测试机参数:

这里写图片描述

service:是一个后台服务,专门用来处理常驻后台的工作的组件。

一、优先级

进程的重要性优先级:(越往后的就越容易被系统杀死)
1.前台进程;Foreground process
1)用户正在交互的Activity(onResume())
2)当某个Service绑定正在交互的Activity。
3)被主动调用为前台Service(startForeground())
4)组件正在执行生命周期的回调(onCreate()/onStart()/onDestroy())
5)BroadcastReceiver 正在执行onReceive();

2.可见进程;Visible process
1)我们的Activity处在onPause()(没有进入onStop())
2)绑定到前台Activity的Service。

3.服务进程;Service process
简单的startService()启动。
4.后台进程;Background process
对用户没有直接影响的进程—-Activity出于onStop()的时候。
android:process=”:xxx”
5.空进程; Empty process
不含有任何的活动的组件。(android设计的,为了第二次启动更快,采取的一个权衡)

好了,基础知识回顾的差不多了!进入正题吧!

事先声明哈!其实是没有真正的进程的保活的,在手动杀死后台的时候,运行在系统后台的所有进程都是一一被杀死的,注意的是,一个一个被杀死的,所以我们才利用这个特性来做的!额。。中心思想即是,在应用被打开的时候,启动两个后台服务,这两个后台服务是相互依存的,也就是说,当一个进程被干掉的时候,另一个存活的进程就立马将其拉起唤醒,也就是打一个时间差!

嗯,上代码!

远程服务代码:

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.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class RemoteService extends Service {

public static final String TAG = "tianchuangxin";
private MyBinder binder;
private MyServiceConnection conn;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
if(binder ==null){
binder = new MyBinder();
}
cOnn= new MyServiceConnection();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);

PendingIntent cOntentIntent= PendingIntent.getService(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("XXX")
.setContentIntent(contentIntent)
.setContentTitle("我是XXX,我怕谁!")
.setAutoCancel(true)
.setContentText("哈哈")
.setWhen( System.currentTimeMillis());

//把service设置为前台运行,避免手机系统自动杀掉改服务。
startForeground(startId, builder.build());
return START_STICKY;
}

class MyBinder extends RemoteConnection.Stub{

@Override
public String getProcessName() throws RemoteException {
// TODO Auto-generated method stub
return "LocalService";
}

}

class MyServiceConnection implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "建立连接成功!");

}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "LocalService服务被干掉了~~~~断开连接!");
Toast.makeText(RemoteService.this, "断开连接", 0).show();
//启动被干掉的
RemoteService.this.startService(new Intent(RemoteService.this, LocalService.class));
RemoteService.this.bindService(new Intent(RemoteService.this, LocalService.class), conn, Context.BIND_IMPORTANT);
}

}


}

本地服务代码:

import com.dn.keepliveprocess.RemoteService.MyBinder;
import com.dn.keepliveprocess.RemoteService.MyServiceConnection;

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.IBinder;
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class LocalService extends Service {

public static final String TAG = "tianchaungxin";
private MyBinder binder;
private MyServiceConnection conn;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
if(binder ==null){
binder = new MyBinder();
}
cOnn= new MyServiceConnection();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);

PendingIntent cOntentIntent= PendingIntent.getService(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("XXX")
.setContentIntent(contentIntent)
.setContentTitle("我是XXX,我怕谁!")
.setAutoCancel(true)
.setContentText("哈哈")
.setWhen( System.currentTimeMillis());

//把service设置为前台运行,避免手机系统自动杀掉改服务。
startForeground(startId, builder.build());
return START_STICKY;
}


class MyBinder extends RemoteConnection.Stub{

@Override
public String getProcessName() throws RemoteException {
// TODO Auto-generated method stub
return "LocalService";
}

}

class MyServiceConnection implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "建立连接成功!");

}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "RemoteService服务被干掉了~~~~断开连接!");
Toast.makeText(LocalService.this, "断开连接", 0).show();
//启动被干掉的
LocalService.this.startService(new Intent(LocalService.this, RemoteService.class));
LocalService.this.bindService(new Intent(LocalService.this, RemoteService.class), conn, Context.BIND_IMPORTANT);
}

}


}

JobService保证在息屏后,CPU进入休眠状态时进行唤醒

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("NewApi")
public class JobHandleService extends JobService{
private int kJobId = 0;
@Override
public void onCreate() {
super.onCreate();
Log.i("tianchuangxin", "jobService create");

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("tianchuangxin", "jobService start");
scheduleJob(getJobInfo());
return START_NOT_STICKY;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

@Override
public boolean onStartJob(JobParameters params) {
// TODO Auto-generated method stub
Log.i("tianchuangxin", "job start");
// scheduleJob(getJobInfo());
boolean isLocalServiceWork = isServiceWork(this, 你的本地服务ref----XXXX.LocalService);
boolean isRemoteServiceWork = isServiceWork(this, 你的远程服务ref----XXXX.RemoteService);
// Log.i("INFO", "localSericeWork:"+isLocalServiceWork);
// Log.i("INFO", "remoteSericeWork:"+isRemoteServiceWork);
if(!isLocalServiceWork||
!isRemoteServiceWork){
this.startService(new Intent(this,LocalService.class));
this.startService(new Intent(this,RemoteService.class));
Toast.makeText(this, "process start", Toast.LENGTH_SHORT).show();
}
return true;
}

@Override
public boolean onStopJob(JobParameters params) {
Log.i("tianchuangxin", "job stop");
// Toast.makeText(this, "process stop", Toast.LENGTH_SHORT).show();
scheduleJob(getJobInfo());
return true;
}

/** Send job to the JobScheduler. */
public void scheduleJob(JobInfo t) {
Log.i("tianchuangxin", "Scheduling job");
JobScheduler tm =
(JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
tm.schedule(t);
}

public JobInfo getJobInfo(){
JobInfo.Builder builder = new JobInfo.Builder(kJobId++, new ComponentName(this, JobHandleService.class));
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPersisted(true);
builder.setRequiresCharging(false);
builder.setRequiresDeviceIdle(false);
builder.setPeriodic(10);//间隔时间--周期
return builder.build();
}


/**
* 判断某个服务是否正在运行的方法
*
* @param mContext
* @param serviceName
* 是包名+服务的类名(例如:net.loonggg.testbackstage.TestService)
* @return true代表正在运行,false代表服务没有正在运行
*/

public boolean isServiceWork(Context mContext, String serviceName) {
boolean isWork = false;
ActivityManager myAM = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List myList = myAM.getRunningServices(100);
if (myList.size() <= 0) {
return false;
}
for (int i = 0; i String mName = myList.get(i).service.getClassName().toString();
if (mName.equals(serviceName)) {
isWork = true;
break;
}
}
return isWork;
}
}

AIDL定义


interface RemoteConnection{
String getProcessName();
}

我们再在MainActivity中进行调用:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

startService(new Intent(this, LocalService.class));
startService(new Intent(this, RemoteService.class));
startService(new Intent(this, JobHandleService.class));
}

OK,所有代码基本在这了!哈哈!有问题的话留言交流!

每天进步一点点,时间会让你成为巨人!

我的Github地址:
https://github.com/T-chuangxin


推荐阅读
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 深入理解Kafka服务端请求队列中请求的处理
    本文深入分析了Kafka服务端请求队列中请求的处理过程,详细介绍了请求的封装和放入请求队列的过程,以及处理请求的线程池的创建和容量设置。通过场景分析、图示说明和源码分析,帮助读者更好地理解Kafka服务端的工作原理。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
author-avatar
无棱9931
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有