3 Star 13 Fork 9

江节胜 / AndroidAPP保活

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
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销魂,但是在最近任务栏点击清除所有的按钮或者在桌面点击 "一键清理",一般不会被销毁。

作者:江节胜

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

Q Q :596957738

个人网站:tech.jiangjiesheng.cn

联系邮箱:dev@jiangjiesheng.cn

空文件

简介

AndroidAPP保活,同时封装成依赖库,直接引入gradle依赖,简单几步即可实现APP保活。 展开 收起
取消

发行版 (4)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/jiangjiesheng/androidapp_preservation.git
git@gitee.com:jiangjiesheng/androidapp_preservation.git
jiangjiesheng
androidapp_preservation
AndroidAPP保活
master

搜索帮助