不重新启动前台服务当系统被杀死或用户被强制关闭时,我尝试了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;
答案 0 :(得分:0)
请使用警报服务重新启动服务
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);