【问题标题】:Service is killed after a short period of time (1 minute)服务在短时间内(1 分钟)后被终止
【发布时间】:2018-12-29 16:52:35
【问题描述】:

我创建了一个服务,它的工作是在用户关闭应用程序时清除通知。一切运行良好,但有时当应用程序在后台运行超过 1 分钟时,服务会被终止(这意味着通知不会被取消)。 为什么会这样?我认为停止服务的唯一方法是使用 stopSelf() 或 stopService()。

public class OnClearFromRecentService extends Service {
    private static final String TAG = "onClearFromRecentServic";
    private NotificationManagerCompat mNotificationManagerCompat;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Service Started");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service Destroyed");
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        //Put code here which will be executed when app is closed from user.
        Log.d(TAG, "onTaskRemoved was executed ");
        if (mNotificationManagerCompat != null) {
            mNotificationManagerCompat.cancelAll();
        } else {
            Log.d(TAG, "onTaskRemoved: mNotifManager is null!");
        }

        stopSelf();
    }
}

我从启动屏幕 Activity 启动服务,如下所示:startService(new Intent(this, OnClearFromRecentService.class));

这里还有一些日志消息:

【问题讨论】:

  • 哪个版本的安卓? 7.x+?
  • @emandt 我在 API 27 上运行这个应用程序
  • 已经有很多关于这个“问题”的线程。从 SDK 26 开始,您必须创建一个前台服务,否则系统会在很短的时间后将其杀死。链接:developer.android.com/about/versions/oreo/android-8.0-changes
  • @emandt 你是对的。谢谢
  • @emandt 即使我打算使用服务在用户设备中加载文件列表。对我来说,该服务将从初始屏幕开始,并将继续工作直到完成工作。现在我的问题是假设如果用户在服务运行时最小化我的应用程序将在 Android 8.0 上运行,那么操作系统会在 1 分钟后终止服务吗?如果是,那么使用线程会帮助我克服这个问题吗?请帮忙。

标签: android android-components


【解决方案1】:

尝试返回 START_STICKY 表单 onStartCommand。
然后系统将尝试重新创建。
查看this官方文档。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Service Started");
        return START_NOT_STICKY;
    }

如果您还希望重新交付 Intent,您也可以尝试重新发送 START_REDELIVER_INTENT。

START_REDELIVER_INTENT

从 onStartCommand(Intent, int, int) 返回的常量:如果这个 服务的进程在启动时被杀死(从 onStartCommand(Intent, int, int)),那么它将被调度为一个 重新启动,最后交付的 Intent 再次通过 onStartCommand(Intent, int, int).

来自文档。

【讨论】:

  • 即使我改变了问题仍然存在。 @emandt 在评论中给了我答案。
【解决方案2】:

我在@emandt 的帮助下找到了解决方案。

我刚刚在 onStartCommand() 中添加了这些代码行:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Service Started");
    Notification notification = new NotificationCompat.Builder(this, CHANNELID)
            .setContentTitle("title")
            .setContentText("text")
            .setSmallIcon(R.drawable.baseline_pause_white_24)
            .build();
    startForeground(2001,notification);
    return START_NOT_STICKY;
}

根据文档 startForeground 方法:

如果您的服务已启动,那么还要让该服务在前台运行,提供在此状态下向用户显示的持续通知...默认情况下,启动的服务是后台的,这意味着它们的进程不会给定前台 CPU 调度(除非该进程中的其他内容是前台)

还有,

如果您的应用以 API 级别 26 或更高级别为目标,系统会对使用或创建后台服务施加限制,除非应用本身位于前台。如果应用需要创建前台服务,应用应该调用startForegroundService()。该方法创建了一个后台服务,但该方法向系统发出信号,该服务将把自己提升到前台。创建服务后,服务必须在五秒内调用其 startForeground() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2016-04-03
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多