应用程序从应用程序托盘中移除后服务的进程被杀死 [英] The process of the service is killed after the application is removed from the application tray

查看:22
本文介绍了应用程序从应用程序托盘中移除后服务的进程被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一项服务(或重新启动正在运行的服务),使用 :

I am starting a service (or re-starting the running service) when an activity is launched, using :

Intent intent = new Intent(this, MyService.class);开始服务(意图);

稍后基于某些操作,使用

Later on based on certain actions, the same activity binds to the service using

bindService(new Intent(this, MyService.class), mConnection, Context.BIND_AUTO_CREATE);

当活动被销毁时,我调用

And when the activity is destroyed, I call

unbindService(mConnection);

之前,当我从应用程序托盘中杀死相同的活动/应用程序并在正在运行的应用程序下显示消息 1 进程 1 服务正在运行"时,该服务会重新启动.

Earlier, the service used to restart when I killed the same activity/application from the application tray and showed the "message 1 process 1 service running" under running apps.

现在,服务不会在杀死相同的活动/应用程序时重新启动.

Now, the service does not restart on killing the same activity/application.

然后我收到消息0 process 1 service running",这意味着该服务实际上没有运行.

And I get the message "0 process 1 service running", which means the service is actually not running.

服务不会在应用程序关闭时重新启动.我的申请由一项活动组成.在系统启动后启动时,该服务也成功启动.

The service does not restart on application being closed. My application consists of one activity. Also the service is successfully started when launched after a system boot.

为什么当我使用 startService() 启动它时服务的进程会被杀死??

Why does the process of the service gets killed when I start it using startService() ??

编辑

在我从应用程序托盘关闭应用程序后,该服务以前会更早地重新启动.但是现在突然使用相同的代码,它没有.当我关闭它们时,它也会发生在其他应用程序上.例如.

The service used to re-start earlier after i closed the app from the application tray. But now suddenly with the SAME code, it doesn't. It happens with other apps too when i close them. eg.

推荐答案

这是我遇到的一种解决方法,如果在关闭应用程序时服务进程被终止,它可以很好地重新启动服务.在您的服务中,添加以下代码.

Here is a workaround I came across and works well for re-starting a service if its process is killed on closing the application. In your service, add the following code.

我在this 线程.

@Override
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
    AlarmManager.ELAPSED_REALTIME,
    SystemClock.elapsedRealtime() + 1000,
    restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
 }

好像是应用进程被杀的bug.如果进程被终止,服务就没有任何运行意义.

Seems to be a bug that the process of the application is killed. There is no point for a service to run if its process is killed.

这篇关于应用程序从应用程序托盘中移除后服务的进程被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆