簡體   English   中英

如何防止重新啟動正在運行的服務-Android

[英]How to prevent restarting a running Service - Android

我想做長時間的后台工作,我也希望每當用戶去參加活動以及更新通知時,就可以在ui中顯示統計信息的進度。

我以START_STICKY模式啟動服務,然后將其綁定到我的活動並使用公共服務方法運行該過程。

一切正常,直到我從最近的應用程序中關閉了我的應用程序。

它會破壞並重新啟動我正在運行的服務。

那就是問題所在。 “我不希望我的運行服務重新啟動”

我希望我的服務保持運行而不會終止,也不會重新啟動。

我該怎么辦?

為什么操作系統重新啟動正在運行的服務:/您

我嘗試了START_NOT_STICKY,但它也正在關閉服務。

在Android 6+上,當用戶從最新消息中刪除應用程序時,前台服務不會停止。 您可以通過將此代碼添加到onCreate()來使服務成為前台服務:

final Intent launcherIntent = new Intent();
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, launcherIntent, 0);
final Notification.Builder builder = new Notification.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("Test Notification")
        .setWhen(System.currentTimeMillis())
        .setContentIntent(pendingIntent);
final Notification notification;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    notification = builder.build();
}
else {
    //noinspection deprecation
    notification = builder.getNotification();
}
this.startForeground(NOTIFICATION_ID, notification);

在Android 6之前的版本中,當用戶從最新消息中刪除任務時,服務總是會被終止。 除了在onTaskRemoved()徹底關閉之外,您onTaskRemoved()

嘗試使用前台服務:

在啟動服務的方法中:

Intent startIntent = new Intent(MainActivity.this, ForegroundService.class);
startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
startService(startIntent);

現在在onStartCommand()

if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i(LOG_TAG, "Received Start Foreground Intent ");
Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();

了解更多簡單前台服務

或者,您可以嘗試執行以下操作:

onStartCommand()需要返回START_STICKY

在服務的onDestroy方法中重寫:

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("EXIT", "ondestroy!");
    Intent broadcastIntent = new Intent();
    broadcastIntent.putExtra("broadcast.Message", "alarm, need to restart service");
    sendBroadcast(broadcastIntent);
}

現在需要實現廣播接收器:

public class RestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stops! Oooooooooooooppppssssss!!!!");
    context.startService(new Intent(context, YourService.class));
    }
}
check if service is running or not 

if(!isBackgroundServiceRunning(BackgroundServices.class))
        {
            Intent intent = new Intent(this,BackgroundServices.class);
            startService(intent);
        }

private boolean isBackgroundServiceRunning(Class<?> service)
    {
        ActivityManager manager = (ActivityManager)(getApplicationContext().getSystemService(ACTIVITY_SERVICE));

        if (manager != null)
        {
            for(ActivityManager.RunningServiceInfo info : manager.getRunningServices(Integer.MAX_VALUE))
            {
                if(service.getName().equals(info.service.getClassName()))
                    return true;
            }
        }
        return false;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM