继续服务,即使应用程序从最近的应用程序被清除

问题描述:

我有一个小问题。

在我的应用程序,启动服务成功登录后,用户。 previously,服务需要停止,如果应用程序被打死。 (比如,从最近的应用程序列表刷卡删除。)因此,我们使用安卓stopWithTask =真正的。现在,我们需要的服务来运行它,即使它开始了它的任务,从最近的应用程序列表中删除。所以我改变了服务,包括安卓stopWithTask =假。但是,这似乎并没有工作。

In my application, a Service is started after user is logged in successfully. Previously, the service needed to stop if application was killed. (say, removed from Recent application list by swiping.) So we had used android:stopWithTask="true". Now we need the Service to run as it is, even if the Task which started it, is removed from Recent app list. So I changed the Service to include android:stopWithTask="false". But that doesn't seem to work.

相关code:

下面是清单的一部分与服务:

Here is manifest part related to Service:

<service
    android:enabled="true"
    android:name=".MyService"
    android:exported="false"
    android:stopWithTask="false" />

在MyService.java:

In MyService.java:

public class MyService extends AbstractService {

    @Override
    public void onStartService() {
        Intent intent = new Intent(this, MyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new Notification(R.drawable.ic_launcher, "My network services", System.currentTimeMillis());
        notification.setLatestEventInfo(this, "AppName", "Message", pendingIntent);
        startForeground(MY_NOTIFICATION_ID, notification);  
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Toast.makeText(getApplicationContext(), "onTaskRemoved called", Toast.LENGTH_LONG).show();
        System.out.println("onTaskRemoved called");
        super.onTaskRemoved(rootIntent);
    }
}

AbstractService.java 是一个扩展 Sevrice 自定义类:

AbstractService.java is custom class that extends Sevrice:

public abstract class AbstractService extends Service {

    protected final String TAG = this.getClass().getName();

    @Override
    public void onCreate() {
        super.onCreate();
        onStartService();
        Log.i(TAG, "onCreate(): Service Started.");
    }

    @Override
    public final int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStarCommand(): Received id " + startId + ": " + intent);
        return START_STICKY; // run until explicitly stopped.
    }

    @Override
    public final IBinder onBind(Intent intent) {
        return m_messenger.getBinder();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onStopService();
        Log.i(TAG, "Service Stopped.");
    }    

    public abstract void onStartService();
    public abstract void onStopService();
    public abstract void onReceiveMessage(Message msg);

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Toast.makeText(getApplicationContext(), "AS onTaskRemoved called", Toast.LENGTH_LONG).show();
        super.onTaskRemoved(rootIntent);
    }
}

现在,如果我登录的应用程序,为MyService开始。我preSS Home键后,让应用程序移动到后台。现在我要从最近应用程序的列表中的应用程序。在那个时候,我的应该的看到了面包和控制台消息,根据该方法的描述:

Now if I login in the application, MyService is started. After that I press Home button, so application is moved to background. Now I remove the application from Recent Application's list. At that time, I should see the Toast and console message, as per this method's description:

公共无效onTaskRemoved(意向rootIntent)

public void onTaskRemoved (Intent rootIntent)

加在API级别14

此被称为如果服务正在运行,并且用户具有   除去来自该服务的应用的任务。如果你有   设置ServiceInfo.FLAG_STOP_WITH_TASK那么您将不会收到此   回电话;相反,服务将简单地被停止。

This is called if the service is currently running and the user has removed a task that comes from the service's application. If you have set ServiceInfo.FLAG_STOP_WITH_TASK then you will not receive this callback; instead, the service will simply be stopped.

参数rootIntent用于该原始根意图   启动正被删除的任务。

Parameters rootIntent The original root Intent that was used to launch the task that is being removed.

不过,我没有看到任何这一点。服务正在恢复START_STICKY在 onStartCommand ,所以我觉得 onTaskRemoved 伴随标志应该被解雇安卓:stopWithTask =假

But I am not seeing any of that. Service is returning START_STICKY in onStartCommand, So I think onTaskRemoved should be fired along with flag android:stopWithTask="false".

我缺少什么?

让我知道,如果我需要添加一些code弄清楚什么是错的,这可能是非常重要的。

Let me know in case I need to add some code which might be important to figure out what's wrong.

PS:我测试了4.2.2到现在

P.S.: I tested this on 4.2.2 till now.

PS:我只是测试相同的code 4.1.2,在其服务保持运行,而我得到的消息onTaskRemoved称为日志,太

P.S.: I just tested the same code in 4.1.2, on which Service keeps running, and I get the message "onTaskRemoved called" in log, too.

我应该怎么做,使这项工作在所有版本的?

What should I do to make this work in all versions?

如果您从应用程序类的子类绑定到你的服务,并坚持自己的IBinder连接,该服务将保持活动的应用程序从删除,即使最近的应用程序。

If you bind to your service from a subclass of Application class and hold on to your IBinder connection, the service will remain alive even after the application is removed from the recent apps.