{"id":21336140,"url":"https://github.com/lcodecorex/keepalive","last_synced_at":"2025-04-05T05:06:45.247Z","repository":{"id":55067602,"uuid":"251319407","full_name":"lcodecorex/KeepAlive","owner":"lcodecorex","description":"Fighting against force-stop kill process on Android with binder ioctl / Android高级保活","archived":false,"fork":false,"pushed_at":"2020-05-30T10:35:10.000Z","size":2465,"stargazers_count":591,"open_issues_count":8,"forks_count":194,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-03-29T04:08:01.669Z","etag":null,"topics":["activity","am","android","binder","ioctl","java","jni","keepalive","ndk","process"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lcodecorex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-30T13:50:39.000Z","updated_at":"2025-03-27T19:49:54.000Z","dependencies_parsed_at":"2022-08-14T11:00:25.061Z","dependency_job_id":null,"html_url":"https://github.com/lcodecorex/KeepAlive","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcodecorex%2FKeepAlive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcodecorex%2FKeepAlive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcodecorex%2FKeepAlive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lcodecorex%2FKeepAlive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lcodecorex","download_url":"https://codeload.github.com/lcodecorex/KeepAlive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289428,"owners_count":20914464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activity","am","android","binder","ioctl","java","jni","keepalive","ndk","process"],"created_at":"2024-11-21T23:51:19.781Z","updated_at":"2025-04-05T05:06:45.227Z","avatar_url":"https://github.com/lcodecorex.png","language":"C","readme":"# KeepAlive\nKeepAlive是在[Leoric](https://github.com/tiann/Leoric)(通过JNI复活进程)的基础上，实现了通过ioctl复活进程，能最大程度提高复活率。\n\n`master`分支是`利用 libbinder.so 与 ActivityManagerService 通信`的版本，`ioctl`分支是`使用 ioctl 与 binder 驱动通信`的版本。\n\n**注**：\n1. 该项目仅供学习和参考，在android4.4到android9.0的模拟器上有效，在真机上不能保证保活成功（MIUI等定制系统已封杀了这个方案）。\n2. 对于自研轻量定制的 Android系统，对一些系统应用的保活，这个方案还是很有优势的。资源占用少，用户无感知，成功率高。\n3. 不建议在C端产品上使用。\n4. 可作为学习binder框架的一个案例。\n\n## 使用方法\n1. 在Application中注册KeepAlive服务\n```\n@Override\nprotected void attachBaseContext(Context base) {\n    super.attachBaseContext(base);\n    KeepAliveConfigs configs = new KeepAliveConfigs(\n                    new KeepAliveConfigs.Config(getPackageName() + \":resident\",\n                            Service1.class.getCanonicalName()));\n    KeepAlive.init(base, configs);\n}\n```\n\n2. Service1对应的进程名是\":resident\"，或者其它任意命名\n```\n\u003cservice\n    android:name=\"Service1\"\n    android:process=\":resident\" /\u003e\n```\nService需要继承KeepAliveService，否则在Android4.4上将没有保活效果。\n\n3. 在合适的地方，启动Service1，它将自动唤醒保活进程\n```\nstartService(new Intent(MainActivity.this, Service1.class));\n```\n如果需要服务自启动，看第6条。\n\n4. 忽略电池优化\n```\nconfigs.ignoreBatteryOptimization();\n```\n\n5. 防止短时间内重复启动\n```\n// 配置短时间重启限制，每次重启间隔限制是10s，最多允许3次10秒内的连续重启\nconfigs.rebootThreshold(10*1000, 3);\n```\n注：保活和重启限制相违背，更准确的应该做崩溃重启限制。\n\n6. 设置应用自启执行的操作\n```\nconfigs.setOnBootReceivedListener(new KeepAliveConfigs.OnBootReceivedListener() {\n    @Override\n    public void onReceive(Context context, Intent intent) {\n        // 设置服务自启\n        context.startService(new Intent(context, Service1.class));\n    }\n});\n```\n\n## 实现原理\n\n- [Android 黑科技保活实现原理揭秘](http://weishu.me/2020/01/16/a-keep-alive-method-on-android/)\n- [深度剖析App保活案例](http://www.52im.net/forum.php?mod=viewthread\u0026tid=2893\u0026highlight=%B1%A3%BB%EE)\n- [Android黑科技保活的技术实现](https://juejin.im/post/5e820b61e51d45470652e7b8)\n\n## 应对方法\n\n下面是一种简单的方法杀死 KeepAlive:\n\n```\nps -A | grep `ps -A | grep keepalive | awk '{print $1}' | head -1` | awk '{print $2}' | xargs kill -19 \u0026\u0026 am force-stop com.boolbird.keepalive\n```\n\n对于系统有两种思路可以选择：\n\n1. 加入在 force-stop 期间不允许启动新的进程的逻辑\n2. 修改 force-stop 的杀进程逻辑为：预先收集好所有进程再进行 kill（如有必要还可以先发送 SIGSTOP）\n\n## 测试\n项目根目录下的kill_alive.sh用于重复杀进程测试。\n\n## 问题\n1、怎么保活多个进程\n2、避免在Application中初始化第三方库，避免在所有进程都初始化第三方库","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flcodecorex%2Fkeepalive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flcodecorex%2Fkeepalive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flcodecorex%2Fkeepalive/lists"}