【Android】一种提高Android应用进程存活率新方法
【Android】一种提高Android应用进程存活率新方法
SkySeraph Jun. 19st 2016
Email:skyseraph00@163.com
更多精彩请直接访问SkySeraph个人站点:www.skyseraph.com
一、基础知识
1.Android 进程优先级
1.1 进程优先级等级一般分法:
- Activte process
- Visible Process
- Service process
- Background process
- Empty process
1.2 进程优先级号
- // Adjustment used in certain places where we don't know it yet.
- // (Generally this is something that is going to be cached, but we
- // don't know the exact value in the cached range to assign yet.)
- static final int UNKNOWN_ADJ = 16;
- // This is a process only hosting activities that are not visible,
- // so it can be killed without any disruption.
- static final int CACHED_APP_MAX_ADJ = 15;
- static final int CACHED_APP_MIN_ADJ = 9;
- // The B list of SERVICE_ADJ -- these are the old and decrepit
- // services that aren't as shiny and interesting as the ones in the A list.
- static final int SERVICE_B_ADJ = 8;
- // This is the process of the previous application that the user was in.
- // This process is kept above other things, because it is very common to
- // switch back to the previous app. This is important both for recent
- // task switch (toggling between the two top recent apps) as well as normal
- // UI flow such as clicking on a URI in the e-mail app to view in the browser,
- // and then pressing back to return to e-mail.
- static final int PREVIOUS_APP_ADJ = 7;
- // This is a process holding the home application -- we want to try
- // avoiding killing it, even if it would normally be in the background,
- // because the user interacts with it so much.
- static final int HOME_APP_ADJ = 6;
- // This is a process holding an application service -- killing it will not
- // have much of an impact as far as the user is concerned.
- static final int SERVICE_ADJ = 5;
- // This is a process with a heavy-weight application. It is in the
- // background, but we want to try to avoid killing it. Value set in
- // system/rootdir/init.rc on startup.
- static final int HEAVY_WEIGHT_APP_ADJ = 4;
- // This is a process currently hosting a backup operation. Killing it
- // is not entirely fatal but is generally a bad idea.
- static final int BACKUP_APP_ADJ = 3;
- // This is a process only hosting components that are perceptible to the
- // user, and we really want to avoid killing them, but they are not
- // immediately visible. An example is background music playback.
- static final int PERCEPTIBLE_APP_ADJ = 2;
- // This is a process only hosting activities that are visible to the
- // user, so we'd prefer they don't disappear.
- static final int VISIBLE_APP_ADJ = 1;
- // This is the process running the current foreground app. We'd really
- // rather not kill it!
- static final int FOREGROUND_APP_ADJ = 0;
- // This is a process that the system or a persistent process has bound to,
- // and indicated it is important.
- static final int PERSISTENT_SERVICE_ADJ = -11;
- // This is a system persistent process, such as telephony. Definitely
- // don't want to kill it, but doing so is not completely fatal.
- static final int PERSISTENT_PROC_ADJ = -12;
- // The system process runs at the default adjustment.
- static final int SYSTEM_ADJ = -16;
- // Special code for native processes that are not being managed by the system (so
- // don't have an oom adj assigned by the system).
- static final int NATIVE_ADJ = -17;
2. Android Low Memory Killer
Android系统内存不足时,系统会杀掉一部分进程以释放空间,谁生谁死的这个生死大权就是由LMK所决定的,这就是Android系统中的Low Memory Killer,其基于Linux的OOM机制,其阈值定义如下面所示的lowmemorykiller文件中,当然也可以通过系统的init.rc实现自定义。
lowmemorykiller.c
- static uint32_t lowmem_debug_level = ;
- static int lowmem_adj[] = {
- ,
- ,
- ,
- ,
- };
- static int lowmem_adj_size = ;
- static int lowmem_minfree[] = {
- * , /* 6MB */
- * , /* 8MB */
- * , /* 16MB */
- * , /* 64MB */
- };
- static int lowmem_minfree_size = ;
① 在Low Memory Killer中通过进程的oom_adj与占用内存的大小决定要杀死的进程,oom_adj值越小越不容易被杀死。其中,lowmem_minfree是杀进程的时机,谁被杀,则取决于lowmem_adj,具体值得含义参考上面 Android进程优先级 所述.
② 在init.rc中定义了init进程(系统进程)的oom_adj为-16,其不可能会被杀死(init的PID是),而前台进程是(这里的前台进程是指用户正在使用的Activity所在的进程),用户按Home键回到桌面时的优先级是6,普通的Service的进程是8.
init.rc
- # Set init and its forked children's oom_adj.
- write /proc//oom_adj -
关于Low Memory Killer的具体实现原理可参考Ref-2.
3. 查看某个App的进程
步骤(手机与PC连接)
1. adb shell
2. ps | grep 进程名
3. cat /proc/pid/oom_adj //其中pid是上述grep得到的进程号
4. Android账号和同步机制
属于Android中较偏冷的知识,具体参考 Ref 3/4/5
二、现有方法
1. 网络连接保活方法
a. GCM
b. 公共的第三方push通道(信鸽等)
c. 自身跟服务器通过轮询,或者长连接
具体实现请参考 微信架构师杨干荣的"微信Android客户端后台保活经验分享" (Ref-1).
2. 双service 提高进程优先级
思路:(API level > 18 )
① 应用启动时启动一个假的Service(FakeService), startForeground(),传一个空的Notification
② 启动真正的Service(AlwaysLiveService),startForeground(),注意必须相同Notification ID
③ FakeService stopForeground()
效果:通过adb查看,运行在后台的服务其进程号变成了1(优先级仅次于前台进程)
风险:Android系统前台service的一个漏洞,可能在6.0以上系统中修复
实现:核心代码如下
AlwaysLiveService 常驻内存服务
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- startForeground(R.id.notify, new Notification());
- startService(new Intent(this, FakeService.class));
- return super.onStartCommand(intent, flags, startId);
- }
FakeService 临时服务
- public class FakeService extends Service {
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- startForeground(R.id.notify, new Notification());
- stopSelf();
- return super.onStartCommand(intent, flags, startId);
- }
- @Override
- public void onDestroy() {
- stopForeground(true);
- super.onDestroy();
- }
- }
3. 守护进程及时拉起
AlarmReceiver, ConnectReceiver,BootReceiver等
三、新方法(AccountSyncAdapter)
1. 思路:
利用Android系统提供的账号和同步机制实现
2. 效果:
① 通过adb查看,运行在后台的服务其进程号变成了1(优先级仅次于前台进程),能提高进程优先级,对比如下图
正常情况
采用AccountSyncAdapter方法后
② 进程被系统kill后,可以由syn拉起
3. 风险:
① SyncAdapter时间进度不高,往往会因为手机处于休眠状态,而时间往后调整,同步间隔最低为1分钟
② 用户可以单独停止或者删除,有些手机账号默认是不同步的,需要手动开启
4. 实现:核心代码如下
① 建立数据同步系统(ContentProvider)
通过一个ContentProvider用来作数据同步,由于并没有实际数据同步,所以此处就直接建立一个空的ContentProvider即可
- public class XXAccountProvider extends ContentProvider {
- public static final String AUTHORITY = "包名.provider";
- public static final String CONTENT_URI_BASE = "content://" + AUTHORITY;
- public static final String TABLE_NAME = "data";
- public static final Uri CONTENT_URI = Uri.parse(CONTENT_URI_BASE + "/" + TABLE_NAME);
- @Override
- public boolean onCreate() {
- return true;
- }
- @Nullable
- @Override
- public Cursor query(Uri uri, String[] projection, String selection,
- String[] selectionArgs, String sortOrder) {
- return null;
- }
- @Nullable
- @Override
- public String getType(Uri uri) {
- return new String();
- }
- @Nullable
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- return null;
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- return 0;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
- return 0;
- }
- }
然后再Manifest中声明
- <provider
- android:name="**.XXAccountProvider"
- android:authorities="@string/account_auth_provider"
- android:exported="false"
- android:syncable="true"/>
② 建立Sync系统 (SyncAdapter)
通过实现SyncAdapter这个系统服务后, 利用系统的定时器对程序数据ContentProvider进行更新,具体步骤为:
- 创建Sync服务
- public class XXSyncService extends Service {
- private static final Object sSyncAdapterLock = new Object();
- private static XXSyncAdapter sSyncAdapter = null;
- @Override
- public void onCreate() {
- synchronized (sSyncAdapterLock) {
- if (sSyncAdapter == null) {
- sSyncAdapter = new XXSyncAdapter(getApplicationContext(), true);
- }
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- return sSyncAdapter.getSyncAdapterBinder();
- }
- static class XXSyncAdapter extends AbstractThreadedSyncAdapter {
- public XXSyncAdapter(Context context, boolean autoInitialize) {
- super(context, autoInitialize);
- }
- @Override
- public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
- getContext().getContentResolver().notifyChange(XXAccountProvider.CONTENT_URI, null, false);
- }
- }
- }
- 声明Sync服务
- <service
- android:name="**.XXSyncService"
- android:exported="true"
- android:process=":core">
- <intent-filter>
- <action
- android:name="android.content.SyncAdapter"/>
- </intent-filter>
- <meta-data
- android:name="android.content.SyncAdapter"
- android:resource="@xml/sync_adapter"/>
- </service>
其中sync_adapter为:
- <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
- android:accountType="@string/account_auth_type"
- android:allowParallelSyncs="false"
- android:contentAuthority="@string/account_auth_provide"
- android:isAlwaysSyncable="true"
- android:supportsUploading="false"
- android:userVisible="true"/>
参数说明:
android:contentAuthority 指定要同步的ContentProvider在其AndroidManifest.xml文件中有个android:authorities属性。
android:accountType 表示进行同步的账号的类型。
android:userVisible 设置是否在“设置”中显示
android:supportsUploading 设置是否必须notifyChange通知才能同步
android:allowParallelSyncs 是否支持多账号同时同步
android:isAlwaysSyncable 设置所有账号的isSyncable为1
android:syncAdapterSettingsAction 指定一个可以设置同步的activity的Action。
- 账户调用Sync服务
首先配置好Account(第三步),然后再通过ContentProvider实现
手动更新
- public void triggerRefresh() {
- Bundle b = new Bundle();
- b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
- b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
- ContentResolver.requestSync(
- account,
- CONTENT_AUTHORITY,
- b);
- }
添加账号
- Account account = AccountService.GetAccount();
- AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
- accountManager.addAccountExplicitly(...)
同步周期设置
- ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
- ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
- ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);
③ 建立账号系统 (Account Authenticator)
通过建立Account账号,并关联SyncAdapter服务实现同步
- 创建Account服务
- public class XXAuthService extends Service {
- private XXAuthenticator mAuthenticator;
- @Override
- public void onCreate() {
- mAuthenticator = new XXAuthenticator(this);
- }
- private XXAuthenticator getAuthenticator() {
- if (mAuthenticator == null)
- mAuthenticator = new XXAuthenticator(this);
- return mAuthenticator;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return getAuthenticator().getIBinder();
- }
- class XXAuthenticator extends AbstractAccountAuthenticator {
- private final Context context;
- private AccountManager accountManager;
- public XXAuthenticator(Context context) {
- super(context);
- this.context = context;
- accountManager = AccountManager.get(context);
- }
- @Override
- public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options)
- throws NetworkErrorException {
- // 添加账号 示例代码
- final Bundle bundle = new Bundle();
- final Intent intent = new Intent(context, AuthActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
- @Override
- public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
- throws NetworkErrorException {
- // 认证 示例代码
- String authToken = accountManager.peekAuthToken(account, getString(R.string.account_token_type));
- //if not, might be expired, register again
- if (TextUtils.isEmpty(authToken)) {
- final String password = accountManager.getPassword(account);
- if (password != null) {
- //get new token
- authToken = account.name + password;
- }
- }
- //without password, need to sign again
- final Bundle bundle = new Bundle();
- if (!TextUtils.isEmpty(authToken)) {
- bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
- bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
- bundle.putString(AccountManager.KEY_AUTHTOKEN, authToken);
- return bundle;
- }
- //no account data at all, need to do a sign
- final Intent intent = new Intent(context, AuthActivity.class);
- intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
- intent.putExtra(AuthActivity.ARG_ACCOUNT_NAME, account.name);
- bundle.putParcelable(AccountManager.KEY_INTENT, intent);
- return bundle;
- }
- @Override
- public String getAuthTokenLabel(String authTokenType) {
- // throw new UnsupportedOperationException();
- return null;
- }
- @Override
- public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
- return null;
- }
- @Override
- public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options)
- throws NetworkErrorException {
- return null;
- }
- @Override
- public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options)
- throws NetworkErrorException {
- return null;
- }
- @Override
- public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features)
- throws NetworkErrorException {
- return null;
- }
- }
- }
- 声明Account服务
- <service
- android:name="**.XXAuthService"
- android:exported="true"
- android:process=":core">
- <intent-filter>
- <action
- android:name="android.accounts.AccountAuthenticator"/>
- </intent-filter>
- <meta-data
- android:name="android.accounts.AccountAuthenticator"
- android:resource="@xml/authenticator"/>
- </service>
其中authenticator为:
- <?xml version="1.0" encoding="utf-8"?>
- <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
- android:accountType="@string/account_auth_type"
- android:icon="@drawable/icon"
- android:smallIcon="@drawable/icon"
- android:label="@string/app_name"
- />
- 使用Account服务
同SyncAdapter,通过AccountManager使用
- 申请Token主要是通过 [AccountManager.getAuthToken]系列方法
- 添加账号则通过 [AccountManager.addAccount]
- 查看是否存在账号通过 [AccountManager.getAccountsByType]
Refs
1. [微信Android客户端后台保活经验分享]
2. [Android Low Memory Killer原理]
3. [stackOverflow 上介绍的双Service方法]
4. [Write your own Android Sync Adapter]
5. [Write your own Android Authenticator]
6. Android developer
- [android.accounts]
- [AccountManager]
- [AbstractAccountAuthenticator]
- [AccountAuthenticatorActivity]
- [Creating a Sync Adapter]
========
By SkySeraph-2016 www.skyseraph.com
【Android】一种提高Android应用进程存活率新方法的更多相关文章
- 一种提高Android应用进程存活率新方法
一.基础知识 1.Android 进程优先级 1.1 进程优先级等级一般分法:- Activte process- Visible Process- Service process- Backgrou ...
- android 8种对话框(Dialog)使用方法汇总
1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...
- 关于如何提高SRAM存储器的新方法
SRAM是当今处理器上最普遍的内存.当芯片制造商宣布他们已经成功地将更多的电路封装到芯片上时,通常是较小的晶体管引起了人们的注意.但是连接晶体管形成电路的互连也必须收缩.IMEC的研究人员提出了一个方 ...
- [Android]GOF23种设计模式 & Android中的设计模式
GOF23种设计模式 设计原则: 1. 单一职责原则(SRP):就一个类而言,应该仅有一个引起它变化的原因 2. 开放-封闭原则(OCP):软件实体(类.模块.函数等)应该可以扩展,但是不可修改.即对 ...
- 一种部署 Python 代码的新方法
在Nylas,我们喜欢使用Python进行开发.它的语法简单并富有表现力,拥有大量可用的开源模块和框架,而且这个社区既受欢迎又有多样性.我们的后台是纯用 Python 写的,团队也经常在 PyCon ...
- NFV一种提高进程消息高可用性的方法
1.背景及概述 1.1 背景 在做NFV的过程中,由于控制面进程被放置到不同虚拟机中,中间可能跨越路由器,因此期间网络有可能震荡,这种情况下保证高可用性就必须有保护机制,本文正是在这种背景下的考虑. ...
- 重点:怎样正确的使用QThread类(注:包括推荐使用QThread线程的新方法QObject::moveToThread)
背景描述: 以前,继承 QThread 重新实现 run() 函数是使用 QThread唯一推荐的使用方法.这是相当直观和易于使用的.但是在工作线程中使用槽机制和Qt事件循环时,一些用户使用错了.Qt ...
- Android 三种动画详解
[工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.点我开始Android技术交流] 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让 ...
- 【Android 复习】:Android五种布局的使用方法
---恢复内容开始--- 在Android布局中,有五种常用的布局,下面我们就来学习一下这几种布局的使用方式 1) 线性布局:LinearLayout 2) 帧布局: FrameLayout 3) ...
随机推荐
- oracle 序列中cache 有什么用途
create sequence name increment by x //x为增长间隔 start with x //x为初始值 maxvalue x //x为最大值 minvalue x //x为 ...
- db2、Oracle存储过程引号用法
在存储过程中,单引号有两个作用,一是字符串是由单引号引用,二是转义.单引号的使用是就近配对,即就近原则.而在单引号充当转义角色时相对不好理解 1.从第二个单引号开始被视为转义符,如果第二个 ...
- 爬取https页面遇到“SSLError: hostname 'xxx' doesn't match either of”的解决方法
使用python requests 框架包访问https://itunes.apple.com 页面是遇到 SSLError: hostname 'itunes.apple.com' doesn't ...
- centos7 打开mysql 3306端口并 设置外部访问
mysql安装后默认是localhost访问,如果需要外部访问可以设置一个新的账号把host改为%,意味着所有ip均可以访问 grant all privileges on *.* to 'outUs ...
- javascript实用技巧、javascript高级技巧
字号+作者:H5之家 来源:H5之家 2016-10-31 11:00 我要评论( ) 三零网提供网络编程. JavaScript 的技术文章javascript实用技巧.javascript高级技巧 ...
- CSS属性小结之--半透明处理
项目中经常有遇到需求半透明的情况,如图片.文字.容器.背景等等,每次都要去翻以前的项目,不甚其烦.现在一次性做个小结,方便自己查阅,也同时分享给大家: 一. 元素容器透明 .div{ opacity: ...
- [ActiveMQ]初识ActiveMQ
初识ActiveMQ ActiveMQ介绍 官方网站:http://activemq.apache.org/ 最新版本:ActiveMQ 5.14.1(2016-10-28) 最新版本下载链接:htt ...
- 使用spring的AOP时产生的异常
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' ...
- 爬虫 htmlUnit遇到Cannot locate declared field class org.apache.http.impl.client.HttpClientBuilder.dnsResolve错误
当在使用htmlUnit时遇到无法定位org.apache.http.impl.client.HttpClientBuilder.dnsResolver类时,此时所需要的依赖包为: <depen ...
- Mosquitto搭建Android推送服务(一)MQTT简介
总体概要: MQTT系列文章分为4部分 1.MQTT简介 2.mosquitto服务器搭建 3.编写Mosquitto的可视化工具 4.使用Mosquitto完成Android推送服务 文章钢要: 对 ...