开发系统级应用不被Kill
一.设置方法
(1) 在AndroidManifest中application根节点下,添加如下代码:
android:persistent="true"
(2) 将应用程序push到/system/app或/system/priv-app.
重启之后就会看到效果,当系统将应用杀死之后会被立即重新启动.
二.persistent属性说明
在Android系统中,有一种永久性利用。对应的AndroidManifest.xml文件里,将persistent属性设为true。
这种应用会在系统未准备好时就启动,并在意外挂掉时被重新启动。
1.在系统启动之时,ActivityManagerService的systemReady()会加载所有persistent为true的应用。看如下伪代码:
public void systemReady(final Runnable goingCallback) {
...
List apps = AppGlobals.getPackageManager().getPersistentApplications(STOCK_PM_FLAGS);
if (apps != null) {
int N = apps.size();
int i;
for (i=0; i<N; i++) {
ApplicationInfo info= (ApplicationInfo)apps.get(i);
if (info != null && !info.packageName.equals("android")) {
addAppLocked(info, false, null /* ABI override */);
}
}
}
...
}
// The flags that are set for all calls we make to the package manager.
static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
public List<ApplicationInfo> getPersistentApplications(int flags) {
final ArrayList<ApplicationInfo> finalList = new ArrayList<ApplicationInfo>();
// reader
synchronized (mPackages) {
final Iterator<PackageParser.Package> i = mPackages.values().iterator();
final int userId = UserHandle.getCallingUserId();
while (i.hasNext()) {
final PackageParser.Package p = i.next();
if (p.applicationInfo != null
&& (p.applicationInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0
&& (!mSafeMode || isSystemApp(p))) {
PackageSetting ps = mSettings.mPackages.get(p.packageName);
if (ps != null) {
ApplicationInfo ai = PackageParser.generateApplicationInfo(p, flags,
ps.readUserState(userId), userId);
if (ai != null) {
finalList.add(ai);
}
}
}
}
}
return finalList;
}
final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated,
String abiOverride) {
ProcessRecord app;
if (!isolated) {
app = getProcessRecordLocked(info.processName, info.uid, true);
} else {
app = null;
}
if (app == null) {
app = newProcessRecordLocked(info, null, isolated, 0);
mProcessNames.put(info.processName, app.uid, app);
if (isolated) {
mIsolatedProcesses.put(app.uid, app);
}
updateLruProcessLocked(app, false, null);
updateOomAdjLocked();
}
// This package really, really can not be stopped.
try {
AppGlobals.getPackageManager().setPackageStoppedState(
info.packageName, false, UserHandle.getUserId(app.uid));
} catch (RemoteException e) {
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Failed trying to unstop package "
+ info.packageName + ": " + e);
}
if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
== (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
app.persistent = true;
app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;
}
if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
mPersistentStartingProcesses.add(app);
startProcessLocked(app, "added application", app.processName, abiOverride,
null /* entryPoint */, null /* entryPointArgs */);
}
return app;
}
在systemReady中,调用getPersistentApplications取得带有persistent标志的应用(flags中设置了FLAG_PERSISTENT),
然后调用addAppLocked函数处理包名中不为android的应用。addAppLocked检查应用是否启动,没有启动时启动这个进程,
因此配置了persistent标志的应用会在系统启动时启动。
2.ActivityManagerService会对persistent标志的应用注册监听器:
private final boolean attachApplicationLocked(IApplicationThread thread,
int pid) {
...
AppDeathRecipient adr = new AppDeathRecipient(app, pid, thread);
thread.asBinder().linkToDeath(adr, 0);
app.deathRecipient = adr;
...
}
一旦应用挂掉时检测到,并尝试重新启动。
3.persistent应用可以在系统未准备好时启动:
boolean isAllowedWhileBooting(ApplicationInfo ai)
{
return (ai.flags & ApplicationInfo.FLAG_PERSISTENT) != 0;
}
开发系统级应用不被Kill的更多相关文章
- Go推出的主要目的之一就是G内部大东西太多了,系统级开发巨型项目非常痛苦,Go定位取代C++,Go以简单取胜(KISS)
以前为了做compiler,研读+实现了几乎所有种类的语言.现在看语法手册几乎很快就可以理解整个语言的内容.后来我对比了一下go和rust,发现go的类型系统简直就是拼凑的.这会导致跟C语言一样,需要 ...
- 系统级编程(csapp)
系统级编程漫游 系统级编程提供学生从用户级.程序员的视角认识处理器.网络和操作系统,通过对汇编器和汇编代码.程序性能评测和优化.内存组织层次.网络协议和操作以及并行编程的学习,理解底层计算机系统对应用 ...
- 系统级IO实践学习记录
代码分析 cp1.c 功能:复制文件. #include <stdio.h>#include <stdlib.h>#include <unistd.h>#inclu ...
- Android融合推送MixPush SDK集成多家推送平台,共享系统级推送,杀死APP也能收到推送
消息推送是App运营的重要一环,为了优化消息推送成功率,降低电量和流量消耗,系统级的推送服务显得尤为重要.小米和魅族由此推出了自家的推送平台,在MIUI和Flyme上共享系统级推送服务,让APP在被杀 ...
- 系统级性能分析工具perf的介绍与使用[转]
测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...
- Robot Framework分层、开发系统关键字
开发系统关键字:http://www.cnblogs.com/fnng/p/4261293.html http://www.cnblogs.com/fnng/p/3969978.htm ...
- CentOS:设置系统级代理(转)
原文地址:http://www.cnblogs.com/cocowool/archive/2012/07/05/2578487.html YUM代理设置 编辑/etc/yum.conf,在最后加入 # ...
- 系统级性能分析工具perf的介绍与使用
测试环境:Ubuntu16.04(在VMWare虚拟机使用perf top存在无法显示问题) Kernel:3.13.0-32 系统级性能优化通常包括两个阶段:性能剖析(performance pro ...
- 从service弹出系统级自定义提示框,可在任意页面弹出
添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> // 显示 ...
随机推荐
- 下拉网页div自动浮在顶部
<!DOCTYPE html> <html> <head> <title></title> <style type="tex ...
- LeetCode 98——验证二叉搜索树
1. 题目 2. 解答 2.1. 方法一 我们初始化根节点的范围为长整形数据的最小最大值 \([LONG\_MIN,LONG\_MAX]\),则其左子节点的取值范围为 \([LONG\_MIN,根节点 ...
- Python3 Tkinter-Button
1.绑定事件处理函数 from tkinter import * def hello(): print('Hello!') root=Tk() button=Button(root,text='cli ...
- Window Classes in Win32
探索Win32系统之窗口类(Window Classes in Win32) Kyle MarshMicrosoft Developer Network Technology GroupMSDN技术组 ...
- iOS- 无处不在,详解iOS集成第三方登录(SSO授权登录<无需密码>)
1.前言 不多说,第三登录无处不在!必备技能,今天以新浪微博为例. 这是上次写的iOS第三方社交分享:http://www.cnblogs.com/qingche/p/3727559.html 可 ...
- iOS-UILabel加线
NSAttributedString *attrStr =[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:], ...
- Css入门课程 Css基础
html css javascript三者关系 html是网页内容的载体 css是网页内容的表现,外观控制 javascript是网页逻辑处理和行为控制 css相对于html标签属性的优势 css简化 ...
- HttpServletRequestWrapper 是HttpServletRequest的包装类 ·关系相当于 int 与integer的关系
HttpServletRequestWrapper 是HttpServletRequest的包装类 ·关系相当于 int 与integer的关系
- iOS-开发中的时间处理
做App避免不了要和时间打交道,关于时间的处理,里面有不少门道,远不是一行API调用,获取当前系统时间这么简单.我们需要了解与时间相关的各种API之间的差别,再因场景而异去设计相应的机制. 时间的形式 ...
- C# 面向对象——多态
多态分三种:1.虚方法 2.抽象类 3.接口 1.虚方法1.将父类的方法标记为虚方法 ,使用关键字 virtual,这个函数可以被子类重新写一个遍. 如: class Program { static ...