1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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 {
...
}
}
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();
}
个人网站 tech.jiangjiesheng.cn
QQ: 596957738
微信: 596957738
EMail: dev @jiangjiesheng .com
|
有任何疑问或技术合作都可联系我
微信:yanfahezuo 【推荐】
QQ:596957738