杀死应用

时间:2018-03-12 08:10:27

标签: android service foreground-service

我在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()。即使应用程序被销毁,我也希望服务能够运行,因为它是前台服务,但我发现它有时会运行,有时它会被停止。我不会有任何其他应用程序打开,我的设备没有内存问题。我的前台服务被停止的可能原因是什么,我该如何解决?

1 个答案:

答案 0 :(得分:-1)

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

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

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