空进程 (Empty process):
不含任何活动应用组件的进程。保留这种进程的的唯一目的是用作缓存,以缩短下次在其中运行组件所需的启动时间。 为使总体系统资源在进程缓存和底层内核缓存之间保持平衡,系统往往会终止这些进程。
进程优先级:
首先空进程是最先被回收的,其次便是后台进程,依次往上,前台进程是最后才会被结束。
Android进程保活
有很多种方法可以实现Android的进程保活,比如通过 1像素且透明Activity提升App进程优先级、 通过设置前台Service提升App进程优先级、 Java层的双进程拉活、 JobScheduler实现、 NDK双进程守护、 使用账户同步拉活、 workmanager实现。
下面这幅图,说明的是:
-
红色部分是容易被回收的进程,属于android进程
-
绿色部分是较难被回收的进程,属于android进程
-
其他部分则不是android进程,也不会被系统回收,一般是ROM自带的app和服务才能拥有
本篇文章介绍的是进程第三种方式:
- 双进程拉活(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所处进程被干掉就重新启动
startService(new Intent(RemoteService.this,LocalService.class));
bindService(new Intent(RemoteService.this,LocalService.class),connection,Context.BIND_IMPORTANT);
}
};
private class RemoteBinder extends Binder{
}
}
<service
android:name=“.RemoteService”
android:enabled=“true”
android:exported=“true”
android:process=“:remote” />
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
09)]
[外链图片转存中…(img-IcgBSccX-1715395118910)]
[外链图片转存中…(img-98d0BAPv-1715395118912)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!