繁体   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