在 API 级别 25(牛轧糖)以上的设备上使用 START_STICKY 服务未重新启动
Service not restarting using START_STICKY on devices above API Level 25(Nougat)
我的应用程序中有一项服务需要在应用程序从最近的任务列表中删除后重新启动。 API 25 级及以下的服务重启,但 25 级及以上的服务不重启 versions.Please 帮我解决这个问题,想知道重启与所有兼容的服务的最佳方法 OS版本。
public class XMPPMainService extends Service {
private static final String TAG = "XMPPMainService";
private static final int RECONNECT_TRY_INTERVAL_MS = 900; // 5 Seconds
private PendingIntent pendingIntent;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, " onCreate ");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Logger.LOGD(TAG, "onDestroy: ");
XMPPManager.shutdown();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
XMPPManager.getInstance(getApplicationContext());
return START_STICKY;
}
}
清单文件:
<service
android:name="com.chatmodule.xmpp.XMPPMainService"
android:enabled="true"
android:stopWithTask="false"
android:exported="false" />`
在您的服务中添加此代码class:-
@Override
public void onTaskRemoved(Intent rootIntent){
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
我的应用程序中有一项服务需要在应用程序从最近的任务列表中删除后重新启动。 API 25 级及以下的服务重启,但 25 级及以上的服务不重启 versions.Please 帮我解决这个问题,想知道重启与所有兼容的服务的最佳方法 OS版本。
public class XMPPMainService extends Service {
private static final String TAG = "XMPPMainService";
private static final int RECONNECT_TRY_INTERVAL_MS = 900; // 5 Seconds
private PendingIntent pendingIntent;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, " onCreate ");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
Logger.LOGD(TAG, "onDestroy: ");
XMPPManager.shutdown();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
XMPPManager.getInstance(getApplicationContext());
return START_STICKY;
}
}
清单文件:
<service
android:name="com.chatmodule.xmpp.XMPPMainService"
android:enabled="true"
android:stopWithTask="false"
android:exported="false" />`
在您的服务中添加此代码class:-
@Override
public void onTaskRemoved(Intent rootIntent){
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}