Android安卓进程保活(三)双进程拉活(Java层)

2024-02-17 17:20

本文主要是介绍Android安卓进程保活(三)双进程拉活(Java层),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android进程保活·双进程拉活(Java层)

Android进程


此文章代码Github有提交https://github.com/NorthernBrain/DoubleService

其它文章

Android安卓进程保活(一)1像素且透明Activity
Android安卓进程保活(二)设置前台Service
Android安卓进程保活(三)双进程拉活(Java层)

首先你要知道Android中的进程以及它的优先级,下面来说明它进程
  1. 前台进程 (Foreground process)
  2. 可见进程 (Visible process)
  3. 服务进程 (Service process)
  4. 后台进程 (Background process)
  5. 空进程 (Empty process)

下面进行解释:


前台进程(Foreground process):
用户当前操作所必需的进程。如果一个进程满足以下任一条件,即视为前台进程:
  • 托管用户正在交互的 Activity(已调用 Activity 的 onResume() 方法)
  • 托管某个 Service,后者绑定到用户正在交互的 Activity
  • 托管正在“前台”运行的 Service(服务已调用 startForeground())
  • 托管正执行一个生命周期回调的 Service(onCreate()、onStart() 或 onDestroy())
  • 托管正执行其 onReceive() 方法的 BroadcastReceiver
        通常,在任意给定时间前台进程都为数不多。只有在内存不足以支持它们同时继续运行这一万不得已的情况下,系统才会终止它们。 此时,设备往往已达到内存分页状态,因此需要终止一些前台进程来确保用户界面正常响应。


可见进程 (Visible process):
没有任何前台组件、但仍会影响用户在屏幕上所见内容的进程。 如果一个进程满足以下任一条件,即视为可见进程:
  • 托管不在前台、但仍对用户可见的 Activity(已调用其 onPause() 方法)。例如,如果前台 Activity 启动了一个对话框,允许在其后显示上一 Activity,则有可能会发生这种情况。
  • 托管绑定到可见(或前台)Activity 的 Service。

可见进程被视为是极其重要的进程,除非为了维持所有前台进程同时运行而必须终止,否则系统不会终止这些进程。


服务进程 (Service process):
正在运行已使用 startService() 方法启动的服务且不属于上述两个更高类别进程的进程。尽管服务进程与用户所见内容没有直接关联,但是它们通常在执行一些用户关心的操作(例如,在后台播放音乐或从网络下载数据)。因此,除非内存不足以维持所有前台进程和可见进程同时运行,否则系统会让服务进程保持运行状态。


后台进程 (Service process):
包含目前对用户不可见的 Activity 的进程(已调用 Activity 的 onStop() 方法)。这些进程对用户体验没有直接影响,系统可能随时终止它们,以回收内存供前台进程、可见进程或服务进程使用。 通常会有很多后台进程在运行,因此它们会保存在 LRU (最近最少使用)列表中,以确保包含用户最近查看的 Activity 的进程最后一个被终止。如果某个 Activity 正确实现了生命周期方法,并保存了其当前状态,则终止其进程不会对用户体验产生明显影响,因为当用户导航回该 Activity 时,Activity 会恢复其所有可见状态。


空进程 (Empty process):
不含任何活动应用组件的进程。保留这种进程的的唯一目的是用作缓存,以缩短下次在其中运行组件所需的启动时间。 为使总体系统资源在进程缓存和底层内核缓存之间保持平衡,系统往往会终止这些进程。


进程优先级:
首先空进程是最先被回收的,其次便是后台进程,依次往上,前台进程是最后才会被结束。


Android进程保活

有很多种方法可以实现Android的进程保活,比如通过  1像素且透明Activity提升App进程优先级通过设置前台Service提升App进程优先级Java层的双进程拉活JobScheduler实现NDK双进程守护使用账户同步拉活workmanager实现

下面这幅图,说明的是:
  • 红色部分是容易被回收的进程,属于android进程
  • 绿色部分是较难被回收的进程,属于android进程
  • 其他部分则不是android进程,也不会被系统回收,一般是ROM自带的app和服务才能拥有

在asdf这里插入图片描述

本篇文章介绍的是进程第三种方式:

  • 双进程拉活(Java层)
双进程拉活(Java层):

当一个进程结束后,立刻调用启动另一个进程,这样实现互相调用,互相启动( 只有在一个进程结束时候才会启动另一个进程)


首先创建LocalService.java继承自Service(android.app.Service):↓

public class LocalService extends Service {@Overridepublic IBinder onBind(Intent intent) {return new LocalBinder();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {/*第一个参数Intent第二个参数ServiceConnection*//***  第三个参数介绍:* Flag for {@link #bindService}: automatically create the service as long* as the binding exists.  Note that while this will create the service,* its {@link android.app.Service#onStartCommand}* method will still only be called due to an* explicit call to {@link #startService}.  Even without that, though,* this still provides you with access to the service object while the* service is created.** <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH},* not supplying this flag would also impact how important the system* consider's the target service's process to be.  When set, the only way* for it to be raised was by binding from a service in which case it will* only be important when that activity is in the foreground.  Now to* achieve this behavior you must explicitly supply the new flag* {@link #BIND_ADJUST_WITH_ACTIVITY}.  For compatibility, old applications* that don't specify {@link #BIND_AUTO_CREATE} will automatically have* the flags {@link #BIND_WAIVE_PRIORITY} and* {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve* the same result.*/bindService(new Intent(this,RemoteService.class),connection,Context.BIND_AUTO_CREATE);return super.onStartCommand(intent, flags, startId);}private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//绑定成功}@Overridepublic void onServiceDisconnected(ComponentName name) {//当RemoteService所处进程被干掉就重新启动startService(new Intent(LocalService.this,RemoteService.class));bindService(new Intent(LocalService.this,RemoteService.class),connection,Context.BIND_IMPORTANT);}};private class LocalBinder extends Binder {}
}

对LocalService在清单文件中进行注册
<service android:name=".LocalService" />

创建RemoteService.java继承自Service(android.app.Service):↓
public class RemoteService extends Service {public RemoteService() {}@Overridepublic IBinder onBind(Intent intent) {return new RemoteBinder();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {/*第一个参数Intent第二个参数ServiceConnection第三个参数介绍:/*** Flag for {@link #bindService}: automatically create the service as long* as the binding exists.  Note that while this will create the service,* its {@link android.app.Service#onStartCommand}* method will still only be called due to an* explicit call to {@link #startService}.  Even without that, though,* this still provides you with access to the service object while the* service is created.** <p>Note that prior to {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH},* not supplying this flag would also impact how important the system* consider's the target service's process to be.  When set, the only way* for it to be raised was by binding from a service in which case it will* only be important when that activity is in the foreground.  Now to* achieve this behavior you must explicitly supply the new flag* {@link #BIND_ADJUST_WITH_ACTIVITY}.  For compatibility, old applications* that don't specify {@link #BIND_AUTO_CREATE} will automatically have* the flags {@link #BIND_WAIVE_PRIORITY} and* {@link #BIND_ADJUST_WITH_ACTIVITY} set for them in order to achieve* the same result.*/bindService(new Intent(this,RemoteService.class),connection,Context.BIND_AUTO_CREATE);return super.onStartCommand(intent, flags, startId);}private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {//绑定成功}@Overridepublic void onServiceDisconnected(ComponentName name) {//当RemoteService所处进程被干掉就重新启动startService(new Intent(RemoteService.this,LocalService.class));bindService(new Intent(RemoteService.this,LocalService.class),connection,Context.BIND_IMPORTANT);}};private class RemoteBinder extends Binder{}
}

对RemoteService 在清单文件中进行注册,再制定一个进程名字,好区分
<serviceandroid:name=".RemoteService"android:enabled="true"android:exported="true"android:process=":remote" /><!--android:process=":remote"指定进程名-->


最后在MainActivity启动其中一个服务LocalService:

public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//双进程拉活(Java层)startService(new Intent(this,LocalService.class));}
}

这样就完成了双进程拉活

这篇关于Android安卓进程保活(三)双进程拉活(Java层)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/qq_40881680/article/details/85197044
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/718479

相关文章

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma