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

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

服务进程 (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 {

@Override

public IBinder onBind(Intent intent) {

return new LocalBinder();

}

@Override

public 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.

  • 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() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

//绑定成功

}

@Override

public 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 {

}

}

public class RemoteService extends Service {

public RemoteService() {

}

@Override

public IBinder onBind(Intent intent) {

return new RemoteBinder();

}

@Override

public 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.

  • 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() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

//绑定成功

}

@Override

public void onServiceDisconnected(ComponentName name) {

//当RemoteService所处进程被干掉就重新启动
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

今天关于面试的分享就到这里,还是那句话,有些东西你不仅要懂,而且要能够很好地表达出来,能够让面试官认可你的理解,例如Handler机制,这个是面试必问之题。有些晦涩的点,或许它只活在面试当中,实际工作当中你压根不会用到它,但是你要知道它是什么东西。

最后在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【算法合集】

【延伸Android必备知识点】

【Android部分高级架构视频学习资源】

Android精讲视频领取学习后更加是如虎添翼!进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

-hXYa695I-1713240249614)]

【Android部分高级架构视频学习资源】

Android精讲视频领取学习后更加是如虎添翼!进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值