在服务的过程中,应用程序从应用程序托盘中取出后被杀应用程序、盘中、被杀、过程中

2023-09-12 07:23:39 作者:一缕青烟

我开始一个服务(或重新启动正在运行的服务)时,活动推出后,使用:

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

意向意图=新的意图(这一点,MyService.class);  startService(意向);

Intent intent = new Intent(this, MyService.class); startService(intent);

后来根据某些操作,使用相同的活性绑定到服务

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进程1服务运行,这意味着该服务实际上是没有运行。

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() ??

修改

用于重新启动先前后,我从应用程序托盘关闭应用程序的服务。但现在突然有相同的code,事实并非如此。它发生与其他应用程序也当我关闭它们。例如。

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.

推荐答案

下面是一种变通方法我碰到的和行之有效的重新启动服务,如果它的进程被杀死在关闭应用程序。在服务中,添加以下code。

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.

我在这个线程。

@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);
 }

好像是应用程序的进程被终止的错误。有没有点,如果它的进程被杀死一个服务运行。

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.