【问题标题】:Restart fourground service when System Processer Killed系统进程被杀死时重新启动前台服务
【发布时间】: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


【解决方案1】:

请使用警报服务重新启动您的服务

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2016-10-22
    • 1970-01-01
    相关资源
    最近更新 更多