首页 > 解决方案 > 系统处理器被杀死时重新启动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;

标签: android

解决方案


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

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

推荐阅读