【问题标题】:Foreground service stops on killing app前台服务在杀死应用程序时停止
【发布时间】:2018-03-12 08:10:26
【问题描述】:

我在onStartCommand的服务中添加了以下代码-

    Notification notification = new NotificationCompat.Builder(AbcService.this)
            .setContentTitle("xyz")
            .setTicker("xyz")
            .setContentText("xyz")
            .setSmallIcon(R.drawable.mevo_chat_mevopic)
            .setOngoing(true).build();
    startForeground(AppConstants.ABC,
            notification);

stopForeground(true); 在服务的onDestroy() 内。我希望服务即使在应用程序被销毁时也能运行,因为它是前台服务,但我看到有时它会运行,有时它会停止。我没有任何其他应用程序打开并且我的设备没有内存问题。我的前台服务被停止的可能原因是什么?我该如何解决这个问题?

【问题讨论】:

标签: android service foreground-service


【解决方案1】:

如果您的服务是由您的应用启动的,那么实际上您的服务正在主进程上运行。因此,当应用程序被杀死时,服务也将停止。所以你可以做的是,从你的服务的 onTaskRemoved 方法发送广播,如下所示:

Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);

并有一个广播接收器,它将再次启动服务。我试过了。服务从所有类型的杀戮中重新启动。

【讨论】:

    猜你喜欢
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多