1 Star 1 Fork 2

Irony / KeepAlive

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

KeepAlive

KeepAlive是在Leoric(通过JNI复活进程)的基础上,实现了通过ioctl复活进程,能最大程度提高复活率。

master分支是利用 libbinder.so 与 ActivityManagerService 通信的版本,ioctl分支是使用 ioctl 与 binder 驱动通信的版本。

  1. 该项目仅供学习和参考,在android4.4到android9.0的模拟器上有效,在真机上不能保证保活成功(MIUI等定制系统已封杀了这个方案)。
  2. 对于自研轻量定制的 Android系统,对一些系统应用的保活,这个方案还是很有优势的。资源占用少,用户无感知,成功率高。
  3. 不建议在C端产品上使用。
  4. 可作为学习binder框架的一个案例。

使用方法

  1. 在Application中注册KeepAlive服务
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    KeepAliveConfigs configs = new KeepAliveConfigs(
                    new KeepAliveConfigs.Config(getPackageName() + ":resident",
                            Service1.class.getCanonicalName()));
    KeepAlive.init(base, configs);
}
  1. Service1对应的进程名是":resident",或者其它任意命名
<service
    android:name="Service1"
    android:process=":resident" />

Service需要继承KeepAliveService,否则在Android4.4上将没有保活效果。

  1. 在合适的地方,启动Service1,它将自动唤醒保活进程
startService(new Intent(MainActivity.this, Service1.class));

如果需要服务自启动,看第6条。

  1. 忽略电池优化
configs.ignoreBatteryOptimization();
  1. 防止短时间内重复启动
// 配置短时间重启限制,每次重启间隔限制是10s,最多允许3次10秒内的连续重启
configs.rebootThreshold(10*1000, 3);

注:保活和重启限制相违背,更准确的应该做崩溃重启限制。

  1. 设置应用自启执行的操作
configs.setOnBootReceivedListener(new KeepAliveConfigs.OnBootReceivedListener() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 设置服务自启
        context.startService(new Intent(context, Service1.class));
    }
});

实现原理

应对方法

下面是一种简单的方法杀死 KeepAlive:

ps -A | grep `ps -A | grep keepalive | awk '{print $1}' | head -1` | awk '{print $2}' | xargs kill -19 && am force-stop com.boolbird.keepalive

对于系统有两种思路可以选择:

  1. 加入在 force-stop 期间不允许启动新的进程的逻辑
  2. 修改 force-stop 的杀进程逻辑为:预先收集好所有进程再进行 kill(如有必要还可以先发送 SIGSTOP)

测试

项目根目录下的kill_alive.sh用于重复杀进程测试。

问题

1、怎么保活多个进程 2、避免在Application中初始化第三方库,避免在所有进程都初始化第三方库

MIT License Copyright (c) 2020 lcodecorex Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Fighting against force-stop kill process on Android with binder ioctl / Android高级保活 展开 收起
C 等 4 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/892768447/KeepAlive.git
git@gitee.com:892768447/KeepAlive.git
892768447
KeepAlive
KeepAlive
master

搜索帮助