{"id":19240760,"url":"https://github.com/gyf-dev/cactus","last_synced_at":"2025-05-16T02:09:00.453Z","repository":{"id":42187631,"uuid":"205356662","full_name":"gyf-dev/Cactus","owner":"gyf-dev","description":"Android Keep Alive(安卓保活)，Cactus 集成双进程前台服务，JobScheduler，onePix(一像素)，WorkManager，无声音乐","archived":false,"fork":false,"pushed_at":"2022-08-25T10:07:24.000Z","size":4395,"stargazers_count":1578,"open_issues_count":47,"forks_count":236,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-04-08T12:12:05.889Z","etag":null,"topics":["alive","alived","android","keep","keepalive","keepalived","kotlin"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gyf-dev.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":"2019-08-30T10:07:37.000Z","updated_at":"2025-04-06T22:16:59.000Z","dependencies_parsed_at":"2022-09-11T07:00:12.970Z","dependency_job_id":null,"html_url":"https://github.com/gyf-dev/Cactus","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyf-dev%2FCactus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyf-dev%2FCactus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyf-dev%2FCactus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyf-dev%2FCactus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gyf-dev","download_url":"https://codeload.github.com/gyf-dev/Cactus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254453655,"owners_count":22073617,"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":["alive","alived","android","keep","keepalive","keepalived","kotlin"],"created_at":"2024-11-09T17:08:50.611Z","updated_at":"2025-05-16T02:09:00.437Z","avatar_url":"https://github.com/gyf-dev.png","language":"Kotlin","readme":"## [点我下载demo](https://www.pgyer.com/Cactus)(密码：cactus)\n\n## 注意！注意！注意！写在前面\n  如果你项目里使用了Thread.UncaughtExceptionHandler或者第三方异常捕获库，比如友盟，bugly等，Cactus请在Thread.UncaughtExceptionHandler或者第三方异常捕获库，比如友盟，bugly等之后注册使用，并且建议在Application里注册使用。\n  为什么要这样操作？因为如果android 8.0以上设备隐藏了通知栏信息，当您的app崩溃重启后会出现invalid channel for service notification异常，而该异常属于系统级别的，没法捕获，所以Cactus对该异常进行了杀死app的操作，但是并不能保证第三方异常监控还是能捕获它。\n  如果第三方后台还是有该异常信息，你又觉得该异常影响你的app崩溃率，请调用hideNotificationAfterO(false)方法，打开通知栏信息。\n\n## 使用 \n\u003e android studio\n - androidx\n   ```groovy\n   implementation 'com.gyf.cactus:cactus:1.1.3-beta13'\n   ```\n- android support\n   ```groovy\n   implementation 'com.gyf.cactus:cactus-support:1.1.3-beta13'\n   ```\n\n## 用法（具体api请参考api说明）\n#### java用法\n\n- 注册\n   ```java\n    Cactus.getInstance()\n          .isDebug(true)\n          .setPendingIntent(pendingIntent)\n          .addCallback(new CactusCallback())\n          ... //其他api等\n          ...\n          .register(this)\n   ```\n- 注销\n  ```java\n    Cactus.getInstance().unregister(this)\n   ```  \n- 重启 \n   ```java\n    Cactus.getInstance().restart(this)\n   ```   \n#### kotlin用法\n\n- 注册 \n   ```kotlin\n    cactus {\n       setPendingIntent(pendingIntent)\n       setMusicId(R.raw.main)\n       isDebug(true)\n       ... //其他api等\n       ...\n       addCallback({\n          //onStop回调，可以省略\n       }) { \n          //doWork回调\n       }\n    }\n   ```\n- 注销 \n   ```kotlin\n    cactusUnregister()\n   ``` \n- 重启 \n   ```kotlin\n    cactusRestart()\n   ```      \n## 混淆规则(proguard-rules.pro)\n   ```\n    -keep class com.gyf.cactus.entity.* {*;} \n   ```\n\n## api说明，★ 标识的建议用户修改，而不是使用默认值\n  | api | 说明 | api | 说明 |\n  | :------------- |:-------------:| :------------- | :-------------:|\n  | ★ setChannelId | 渠道Id，默认是Cactus，建议用户修改，非必传 | ★ setChannelName | 渠道名，用于设置里通知渠道展示，默认是Cactus，建议用户修改，非必传 |\n  | ★ setTitle | 通知栏标题，默认是Cactus，建议用户修改，非必传 | ★ setContent |通知栏内容，默认是Cactus is running，建议用户修改，非必传 |\n  | ★ setSmallIcon | 通知栏小图标，默认是库里的图标，建议用户修改，非必传 | setLargeIcon  | 通知栏大图标，默认没有大图标，非必传 |\n  | ★ setServiceId | 服务Id，默认是1到Int.MAX_VALUE随机数，非必传 | setPendingIntent | 设置PendingIntent，用来处理通知栏点击事件，非必传 |\n  | addCallback | 增加回调，用于处理一些额外的工作，非必传 | addBackgroundCallback  | 前后台切换回调，用于处理app前后台切换，非必传 |\n  | setWorkerEnabled | 是否可以使用WorkManager，默认可以使用，非必传 | setCrashRestartUIEnabled  | 奔溃是否可以重启用户界面，默认为false，google原生rom android 10 以下可以正常重启，非必传 |\n  | setRemoteViews | 设置RemoteViews（自定义布局），非必传 | setBigRemoteViews |设置BigRemoteViews（自定义布局），非必传 |\n  | hideNotification | 是否隐藏通知栏，经测试，除了android 7.1手机之外都可以隐藏，默认隐藏，非必传 | hideNotificationAfterO |是否隐藏Android 8.0以上通知栏，默认隐藏 |\n  | setMusicEnabled | 是否可以播放音乐，默认可以播放音乐，非必传 | setBackgroundMusicEnabled  | 后台是否可以播放音乐，默认不可以后台播放音乐，非必传 |\n  | setMusicId | 设置自定义音乐，默认是无声音乐，该api只要在isDebug为true才会有生效，非必传 | ★ setMusicInterval  | 设置音乐间隔时间，时间间隔越长，越省电，默认间隔时间是0，非必传 |\n  | setOnePixEnabled | 是否可以使用一像素，默认可以使用，只有在android p以下可以使用，非必传 | isDebug  | 是否Debug模式，默认没有调试信息，非必传 |\n  | setNotification | 设置notification，非必传，如果不传，将使用用户根据其他api设置的信息构建Notification | setNotificationChannel |设置NotificationChannel，非必传，如果不传，将使用默认的NotificationChannel |\n  | register | 必须调用，建议在Application里初始化，使用Kotlin扩展函数不需要调用此方法 | unregister  | 注销，并不会立马停止，而是在1s之后停止，非必须调用，比如可以在app完全退出的时候可以调用，根据你的需求调用 |\n  | restart | 重启，与register区别在于不会重新配置CactusConfig信息，而是使用上一次配置的信息 | isRunning  | 是否在运行 |\n   \n## 流程图\n![框架流程图](cactus.png)\n\n## 保活效果，仅供参考(数字代码oom_adj优先级，优先级数字越小越不容易被杀)\n  | 维度 | android 6.0以下虚拟机 | android 7.1虚拟机 | android 7/8/8.1/9/10虚拟机 | vovo x23 (android 9) | 华为 mate20 /OnePlus (android 9) | 华为 mate30 pro (android 10) | \n  | :-------------: |:-------------:| :-------------:| :-------------:| :-------------:|:-------------:|:-------------:|\n  | 前台 | 0 | 0 |0 |0 |0 |0 |\n  | 后台（优化前） | 6 | 立马死了 |11 |8 |11 |11 |\n  | 后台（优化后） | 1 | 3 |3 |4 |3 |0 |\n  | 息屏（优化前） | 6 | 立马死了 |11 |9 |11 |11 |\n  | 息屏（优化后） | 0 | 3 |3 |4 |3 |0 |\n- 说明：oom_adj优先级数字越小越不容易被杀\n\n  | oom_adj | 说明 | oom_adj | 说明 |\n  | :-------------: |:-------------:| :-------------:| :-------------:|\n  | 0 | 前台进程 | 1 |可见进程 |\n  | 2 | 可感知的进程，比如那种播放音乐 | 3 |正在备份的进程 |\n  | 4 | 高权重进程 | 5 |有Service的进程 |\n  | 6 | 与Home交互的进程 | 7 |切换进程 |\n  | 8 | 不活跃的进程 | 9 |缓存进程，也就是空进程 |\n  | 11 | 缓存进程，也就是空进程 | 15 |缓存进程，空进程，在内存不足的情况下就会优先被kill |\n  | 16 | 预留的最低级别，一般对于缓存的进程才有可能设置成这个级别 |  | |\n\n## 更新说明\n#### 1.1.2\n- 增加注销和重启功能\n- 增加判断服务是否是在运行中\n- 增加hideNotificationAfterO方法(是否隐藏Android 8.0以上通知栏)\n- 优化代码\n\n#### 1.1.1\n- 重点：修复1.1.0版本由于新增设置渠道api(setNotificationChannel)忘记做渠道判断，导致在8.0以下手机奔溃，1.0.8版本不受影响\n\n#### 1.1.0\n- 除了android7.1手机都可以隐藏通知栏了\n- 增加一些通知栏相关api，比如可以自定义view了\n- 优化代码\n\n#### 1.0.8\n- 解决设置后台可以播放音乐，奔溃重启后无法继续播放音乐的问题\n\n#### 1.0.7\n- 增加前后台切换监听\n- 增加设置后台是否可以播放音乐的api\n\n## 联系我 ##\n- QQ群 314360549（问题交流）","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyf-dev%2Fcactus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgyf-dev%2Fcactus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyf-dev%2Fcactus/lists"}