Skip to content

Commit

Permalink
support keep alive on android 4.4~8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lcodecorex committed Mar 30, 2020
1 parent 6a0f558 commit c18ec9a
Show file tree
Hide file tree
Showing 478 changed files with 56,608 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
116 changes: 116 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# KeepAlive
KeepAlive是在[Leoric](https://github.com/tiann/Leoric)(通过JNI复活进程)的基础上,实现了通过ioctl复活进程,能最大程度提高复活率。

`master`分支是`利用 libbinder.so 与 ActivityManagerService 通信`的版本,`ioctl`分支是`使用 ioctl 与 binder 驱动通信`的版本。

## 使用方法
1. 在Application中注册KeepAlive服务
```
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
KeepAliveConfigs configs = new KeepAliveConfigs(
new KeepAliveConfigs.Config(getPackageName() + ":resident",
Service1.class.getCanonicalName()));
KeepAlive.init(base, configs);
}
```

2. Service1对应的进程名是":resident",或者其它任意命名
```
<service
android:name="Service1"
android:process=":resident" />
```
Service需要继承KeepAliveService,否则在Android4.4上将没有保活效果。

3. 在合适的地方,启动Service1,它将自动唤醒保活进程
```
startService(new Intent(MainActivity.this, Service1.class));
```
如果需要服务自启动,看第6条。

4. 忽略电池优化
```
configs.ignoreBatteryOptimization();
```

5. 防止短时间内重复启动
```
// 配置短时间重启限制,每次重启间隔限制是10s,最多允许3次10秒内的连续重启
configs.rebootThreshold(10*1000, 3);
```
注:保活和重启限制相违背,更准确的应该做崩溃重启限制。

6. 设置应用自启执行的操作
```
configs.setOnBootReceivedListener(new KeepAliveConfigs.OnBootReceivedListener() {
@Override
public void onReceive(Context context, Intent intent) {
// 设置服务自启
context.startService(new Intent(context, Service1.class));
}
});
```

## 实现原理

- [Android 黑科技保活实现原理揭秘](http://weishu.me/2020/01/16/a-keep-alive-method-on-android/)
- [深度剖析App保活案例](http://www.52im.net/forum.php?mod=viewthread&tid=2893&highlight=%B1%A3%BB%EE)

## 应对方法

下面是一种简单的方法杀死 KeepAlive:

```
ps -A | grep `ps -A | grep keepalive | awk '{print $1}' | head -1` | awk '{print $2}' | xargs kill -19 && am force-stop com.boolbird.keepalive
```

对于系统有两种思路可以选择:

1. 加入在 force-stop 期间不允许启动新的进程的逻辑
2. 修改 force-stop 的杀进程逻辑为:预先收集好所有进程再进行 kill(如有必要还可以先发送 SIGSTOP)

## 测试
项目根目录下的kill_alive.sh用于重复杀进程测试。

## 问题
1、怎么保活多个进程
2、避免在Application中初始化第三方库,避免在所有进程都初始化第三方库
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId 'com.boolbird.keepalive'
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(":library")
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
30 changes: 30 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.boolbird.keepalive.demo">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<application
android:name="com.boolbird.keepalive.demo.MainApplication"
android:label="KeepAlive"
android:supportsRtl="true"
android:icon="@mipmap/ic_launcher"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="com.boolbird.keepalive.demo.MainActivity"
android:label="KeepAlive"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name="com.boolbird.keepalive.demo.Service1"
android:process=":resident" />
</application>

</manifest>
15 changes: 15 additions & 0 deletions app/src/main/java/com/boolbird/keepalive/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.boolbird.keepalive.demo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// startService(new Intent(MainActivity.this, Service1.class));
}
}
Loading

0 comments on commit c18ec9a

Please sign in to comment.