# AndroidAPP保活 **Repository Path**: jiangjiesheng/androidapp_preservation ## Basic Information - **Project Name**: AndroidAPP保活 - **Description**: AndroidAPP保活,同时封装成依赖库,直接引入gradle依赖,简单几步即可实现APP保活。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 13 - **Forks**: 9 - **Created**: 2018-07-13 - **Last Updated**: 2024-03-23 ## Categories & Tags **Categories**: android-modules **Tags**: None ## README ### KeepAppAlive 安卓App保活依赖库 源码请看v1.0.0及以上分支版本 #### 使用方法 > 1、gradle引入依赖 Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories: ``` allprojects { repositories { ... maven { url 'https://jitpack.io' } } } ``` Step 2. Add the dependency ``` dependencies { implementation 'com.gitee.jiangjiesheng:androidapp_preservation:v1.0.5' } ``` > 2、首先在Application中初始化 ``` public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); KeepAliveManager.initWithApplicationContext(this.getApplicationContext(),false);//最好放在application中初始化 } } ``` > 3、在需要使用的Activity页面使用:      首先获取实例 ``` KeepAliveManager keepAliveManager = KeepAliveManager.getInstance(this);//获取实例 ```      在onCreate()中设置屏幕监听,添加受保护的Activity ``` @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); keepAliveManager.setKeepAliveManagerStatusListener(new KeepAliveManager.KeepAliveManagerStatusListener() { @Override public void onUserPresent() { Toast.makeText(SportsActivity.this, "用户解锁了", Toast.LENGTH_LONG).show(); } }); keepAliveManager.addRestartActivity(this.getClass()); //这里就是添加了受保护的Activity } ```      开启保活 ``` keepAliveManager.startKeeper(); ```      关闭保活 ``` keepAliveManager.stopKeeper(); ```      在onDestroy()中取消对屏幕的监听 ``` @Override protected void onDestroy() { super.onDestroy(); keepAliveManager.stopScreenListenerOnDestroy(); } ``` **补充说明** - 后期可以增加方法控制不同级别的保活能力,主要是了控制发热和耗电 - 经实测,如果是在最近任务中单独选中这个运行中的app直接拖出任务栏, 会导致app销魂,但是在最近任务栏点击清除所有的按钮或者在桌面点击 "一键清理",一般不会被销毁。 > 作者:[江节胜](https://www.baidu.com/s?wd=%E6%B1%9F%E8%8A%82%E8%83%9C%20%E8%83%9C%E8%A1%8C%E5%A4%A9%E4%B8%8B%E7%BD%91) > 微信:**767000122 (欢迎添加好友)** > Q Q :596957738 > 个人网站:[tech.jiangjiesheng.cn](http://tech.jiangjiesheng.cn) > 联系邮箱:dev@jiangjiesheng.cn