宗旨
本文主要介绍如何在大部分手机上保活,主要是5.0 6.0 7.0和8.0手机。请不要用于恶意目的,仅作学习交流使用。但是至少目前,根据老板安排,世上又多了一个流氓。
不研究被杀重新复活的方式,这种方式会增添开发难度。
API 26查明会在充电时候被优化,但是我们的应用一般是在外面使用,不充电的。可以不用考虑。
1像素Activity保活
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import com.fungshing.map.BMapApiApp;
// 1px activity 为了保活
public class HooliganActivity extends Activity {
private static HooliganActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
Window window = getWindow();
window.setGravity(Gravity.LEFT | Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.height = 1;
params.width = 1;
window.setAttributes(params);
}
/**
* 开启保活页面
*/
public static void startHooligan() {
Intent intent = new Intent(BMapApiApp.getInstance(), HooliganActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
BMapApiApp.getInstance().startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
instance = null;
}
/**
* 关闭保活页面
*/
public static void killHooligan() {
if(instance != null) {
instance.finish();
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.fungshing.HooliganActivity;
import com.fungshing.control.Utils;
import com.fungshing.map.BMapApiApp;
import com.fungshing.service.AssistantService;
/*
* 略带流氓行为的广播接收器
*/
public class HooliganReceiver extends BroadcastReceiver {
private boolean isServiceRunning = false;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
// 如果广播是每分钟发送一次的时间广播
isServiceRunning = Utils.isServiceWorked(BMapApiApp.getInstance(), "AssistantService");
if (!isServiceRunning) {
Intent assistantIntent = new Intent(context, AssistantService.class);
context.startService(assistantIntent);
}
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// 屏幕亮了
isServiceRunning = Utils.isServiceWorked(BMapApiApp.getInstance(), "AssistantService");
if (!isServiceRunning) {
Intent assistantIntent = new Intent(context, AssistantService.class);
context.startService(assistantIntent);
}
HooliganActivity.killHooligan();
}
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// 屏幕灭了
isServiceRunning = Utils.isServiceWorked(BMapApiApp.getInstance(), "AssistantService");
if (!isServiceRunning) {
Intent assistantIntent = new Intent(context, AssistantService.class);
context.startService(assistantIntent);
}
HooliganActivity.startHooligan();
}
}
}
关键是
Intent.ACTION_TIME_TICK
Intent.ACTION_SCREEN_ON
Intent.ACTION_SCREEN_OFF
这三个广播Intent。
https://blog.csdn.net/qq_34161388/article/details/78841838
JobService(API >=21)
https://blog.csdn.net/aqi00/article/details/71638721
双服务,A为本地服务,B为远程服务
自己的服务是本地服务,B为远程服务,要设置成
android:process="remote"
然后通过AIDL的方法连接这两个服务。
A是要保活的服务,B为辅助服务。
https://blog.csdn.net/qq_35980424/article/details/75035821
https://blog.csdn.net/returnnull0/article/details/53750483
AlarmManager
定时服务不断打开所要保活的服务。
https://blog.csdn.net/aqi00/article/details/71638721
Native层(API< 21)
关键在于fork进程,不过这个方法要API低于21才有用。
Android App Daemon:
https://github.com/Coolerfall/Android-AppDaemon
http://coolerfall.com/android/android-app-daemon
MarsDaemon:
https://github.com/Marswin/MarsDaemon
https://blog.csdn.net/marswin89/article/details/50917098
通知保活
目前是显式通知,隐式通知暂未测试。
https://github.com/hadyang/interview/blob/master/android/keep-live.md