1 Star 0 Fork 9

hcxlc / AndroidAPP保活

forked from 江节胜 / AndroidAPP保活 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
README.md 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
jiangjiesheng 提交于 2019-05-19 17:16 . 更换联系方式

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销魂,但是在最近任务栏点击清除所有的按钮或者在桌面点击 "一键清理",一般不会被销毁。

作者:江节胜

微信:767000122 (欢迎添加好友)

Q Q :596957738

个人网站:tech.jiangjiesheng.cn

联系邮箱:dev@jiangjiesheng.cn

1
https://gitee.com/hcxlc/androidapp_preservation.git
git@gitee.com:hcxlc/androidapp_preservation.git
hcxlc
androidapp_preservation
AndroidAPP保活
master

搜索帮助