【发布时间】:2023-04-01 17:13:01
【问题描述】:
不重启前台服务当系统被杀死或用户强制关闭时,我尝试了 OnstartCommand 方法返回粘性意图,并通过广播接收器在 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命令的返回语句 返回 START_STICKY;
【问题讨论】:
-
你的代码一定有问题。
return START_STICKY;将完成这项工作。 -
请更好地解释您的问题到底是什么。
-
我会建议你阅读这篇文章stackoverflow.com/questions/35647788/…
标签: android