你好, friend 们,我已经搜索了很多,但没有找到 Google 所做的服务更改,我需要让我的服务保持活力。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
但它没有在 android 4.4.2 中重新启动服务对此有什么想法吗?
最佳答案
您可以将它用于 Android kitkat,因为它们正在从后台清除进程。
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext()
.getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 5000, restartServicePI);
}
关于android - 如何在 android kitkat 中保持服务活跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24075740/