android - 系统处理器被杀死时重新启动fourground服务
问题描述
前台服务未重新启动当系统被杀死或用户强制关闭时,我尝试了 OnstartCommand 方法 Return Sticky Intent 和,通过广播接收器在 ondestory 方法中调用启动服务,但服务未重新创建或启动
@Override
public void onLowMemory() {
super.onLowMemory();
Intent intent = new Intent("com.lon.loanonmind.loanonmind.MyService");
intent.setClass(this, LocationServices.class);
sendBroadcast(intent);
Log.d("KIll","Service Killed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent intent = new Intent("com.lon.loanonmind.loanonmind.MyService");
intent.setClass(this, LocationServices.class);
sendBroadcast(intent);
Log.d("KIll","Service Killed");
}
@Override
public void onDestroy() {
Intent intent = new Intent("com.lon.loanonmind.loanonmind.MyService");
intent.setClass(this, LocationServices.class);
sendBroadcast(intent);
super.onDestroy();
}
//onstart 命令的返回语句 return START_STICKY;
解决方案
请使用警报服务重新启动您的服务
Intent intent = new Intent("com.lon.loanonmind.loanonmind.MyService");
intent.setClass(this, LocationServices.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent , 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(intent );
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pintent);
推荐阅读
- arrays - (C编程)如何在不使用数组的情况下找到长数中出现频率最高的数字?
- ios - 将您的 Stripe 秘密配置从 sk_test 更新为 sk_live
- xcode - 使用 kotlin 多平台编译 ios 项目时出错
- visual-studio-code - 我可以让 VS Code 跳过打开以前的工作区一次吗?
- r - R:根据列索引将数据帧值从整数转换为双精度
- database - 在 BEGIN..END 过程 PL/SQL 中声明变量时出现编译错误
- javascript - 和弦和歌词格式问题
- reactjs - 我可以创建本地 API 吗?
- java - Create a HashSet with duplicate values
- python - Pandas - Evaluating row wise operation per entity