Android 常驻
常用保活方案:
1、将Service设置为前台进程
2、在service的onStartCommand方法里返回 STATR_STICK
3、添加Manifest文件属性值为android:persistent=“true”
4、覆写Service的onDestroy方法
5、添加广播监听android.intent.action.USER_PRESENT事件以及其他一些可以允许的事件
6、服务互相绑定
7、设置闹钟,定时唤醒
8、账户同步,定时唤醒
9、开启一个1像素activity 前台保活
10、native进程
经过测试我使用service +系统闹钟方式,主要支持在app退回到桌面,锁屏以后保持app存活,小米手机在不手动杀死进程情况下,测试通过(满电到电量耗尽)。
1.权限设置
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DEVICE_POWER"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name = "android.permission.GET_TASKS"/>
2.设置service
package com.osram.jay.com.Andonsystem_New;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.SoundPool;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import java.io.IOException;
import java.util.HashMap;
/**
* 一个定时任务
*/
public class AlarmService extends Service {
/**
* 每1分钟更新一次数据
*/
private static final int ONE_Miniute = 6 * 1000;
private static final int PENDING_REQUEST = 0;
public AlarmService() {
}
/**
* 调用Service都会执行到该方法
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//这里模拟后台操作
//通过AlarmManager定时启动广播
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
long triggerAtTime = SystemClock.elapsedRealtime() + ONE_Miniute;//从开机到现在的毫秒书(手机睡眠(sleep)的时间也包括在内
Intent i = new Intent(this, AlarmReceive.class);
@SuppressLint("WrongConstant") PendingIntent pIntent = PendingIntent.getBroadcast(this, PENDING_REQUEST, i, PENDING_REQUEST);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pIntent);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
3.接受闹钟广播,收到后启动service
package com.osram.jay.com.Andonsystem_New;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//循环启动Service
Intent i = new Intent(context, AlarmService.class);
context.startService(i);
}
}
4.设置省电策略(选择无限制)
public void setBmd() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!pm.isIgnoringBatteryOptimizations(getPackageName())) {
Intent intent = new Intent();
String packageName = getPackageName();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
this.startActivity(intent);
}
}
}
5.设置为自启动模式
6.设置最后退出主页面不要finish()关闭
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent home = new Intent(Intent.ACTION_MAIN);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
return true;
}
return super.onKeyDown(keyCode, event);
}