一:前言
在谈保活之前,我们应该知道随着Android系统的不断发展,系统管控越来越严格,后台保活将是一个伪命题了,后台保活主要是和推送相关,随着技术的发展,泰尔终端实验室也制定了统一的推送标准,我想在后面Android会像Iphone一样,使用统一系统服务进行推送
二:关于Setting模块中关于保活的重要概念
1)开发者选项–不保留活动,即用户离开后清除活动
当一个activity启动的时候,他会关联一个栈,同时栈也有自己的亲和性的;大家可以搜索launch moder了解下大致信息;
这里的不保留活动的意思距离说明是从activity A跳转到activity B,则activity A被销毁,如果此时点击back键,activity A会被重建
在7.0源码的Settings源码目录里面,如果你想通过“不保留活动”关键字去搜索对应代码是搜索不到的,关键代码如下
res/xml/development_prefs.xml
"immediately_destroy_activities"
android:title="@string/immediately_destroy_activities"
android:summary="@string/immediately_destroy_activities_summary"/>
相关类的位置 src/com/android/settings/DevelopmentSettings.java
选中打开“不保留活动”选项执行的代码如下
private void writeImmediatelyDestroyActivitiesOptions() {
try {
ActivityManagerNative.getDefault().setAlwaysFinish(
mImmediatelyDestroyActivities.isChecked());
} catch (RemoteException ex) {
}
}
查看源码得知,setAlwaysFinish如下
public void setAlwaysFinish(boolean enabled) throws RemoteException
{
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
data.writeInt(enabled ? 1 : 0);
mRemote.transact(SET_ALWAYS_FINISH_TRANSACTION, data, reply, 0);
reply.readException();
data.recycle();
reply.recycle();
}
可以看出这个功能是通过往Settings.Global.ALWAYS_FINISH_ACTIVITIES写入数据实现的;
在打开设置这个app的时候,通过读取相关的值来设置开关状态
private void updateImmediatelyDestroyActivitiesOptions() {
updateSwitchPreference(mImmediatelyDestroyActivities, Settings.Global.getInt(
getActivity().getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0) != 0);
}
其中ALWAYS_FINISH_ACTIVITIES为\frameworks\base\core\java\android\provider\Settings.java
/**
* If not 0, the activity manager will aggressively finish activities and
* processes as soon as they are no longer needed. If 0, the normal
* extended lifetime is used.
*/
public static final String ALWAYS_FINISH_ACTIVITIES = "always_finish_activities";
总结:当用户勾选了“不保留活动”这个选项,则会将ALWAYS_FINISH_ACTIVITIES 设置为1,activity manager将在activity不在前台的时候finish掉他
2)限制后台进程
该功能可选的配置有:标准限制,无后台程序,最多1个程序,最多2个程序…
xml配置文件为:res/xml/development_prefs.xml
"app_process_limit"
android:title="@string/app_process_limit_title"
android:entries="@array/app_process_limit_entries"
android:entryValues="@array/app_process_limit_values" />
选择对应的条目之后,相关代码为:src/com/android/settings/DevelopmentSettings.java
private void writeAppProcessLimitOptions(Object newValue) {
try {
int limit = newValue != null ? Integer.parseInt(newValue.toString()) : -1;
ActivityManagerNative.getDefault().setProcessLimit(limit);
updateAppProcessLimitOptions();
} catch (RemoteException e) {
}
}
ActivityManagerNative.getDefault()实现如下:
/**
* Retrieve the system's default/global activity manager.
*/
static public IActivityManager getDefault() {
return gDefault.get();
}
private static final Singleton gDefault = new Singleton() {
protected IActivityManager create() {
IBinder b = ServiceManager.getService("activity");
if (false) {
Log.v("ActivityManager", "default service binder = " + b);
}
IActivityManager am = asInterface(b);
if (false) {
Log.v("ActivityManager", "default service = " + am);
}
return am;
}
};
其中Singleton是一个模板类,通过get()方法返回IActivityManager;
那IActivityManager 的实现是谁尼,我们紧接着分析asInterface(b)
static public IActivityManager asInterface(IBinder obj) {
if (obj == null) {
return null;
}
IActivityManager in =
(IActivityManager)obj.queryLocalInterface(descriptor);
if (in != null) {
return in;
}
return new ActivityManagerProxy(obj);
}
从这里可以看出这里才是真正的返回“in”,只有当in为null的时候,返回ActivityManagerProxy;
这里的IActivityManager的实现者为:ActivityManagerService,最上面的ActivityManagerNative.getDefault().setProcessLimit(limit)其实是调用ActivityManagerService.java的setProcessLimit方法,我们来看一下详细的方法
@Override
public void setProcessLimit(int max) {
enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
"setProcessLimit()");
synchronized (this) {
mProcessLimit = max < 0 ? ProcessList.MAX_CACHED_APPS : max;
mProcessLimitOverride = max;
}
trimApplications();
}
这个方法的主要作用是杀死已经移除的进程,并动态调整oom adj for all processes;
综述:限制后台进程是强行获取android.permission.SET_PROCESS_LIMIT权限,杀死已经移除的进程并动态调整所有进程的oom adj
这里我们并没有解决标准显示到底是限制多少个App,其实答案就在setProcessLimit方法中
mProcessLimit = max < 0 ? ProcessList.MAX_CACHED_APPS : max;
这里的ProcessList.MAX_CACHED_APPS的值为:
// The maximum number of cached processes we will keep around before killing them.
// NOTE: this constant is *only* a control to not let us go too crazy with
// keeping around processes on devices with large amounts of RAM. For devices that
// are tighter on RAM, the out of memory killer is responsible for killing background
// processes as RAM is needed, and we should *never* be relying on this limit to
// kill them. Also note that this limit only applies to cached background processes;
// we have no limit on the number of service, visible, foreground, or other such
// processes and the number of those processes does not count against the cached
// process limit.
static final int MAX_CACHED_APPS = 32;
也就是说标准显示其实是允许保留32个后台进程;