android 清理应用程序,android – 即使应用程序从最近的应用程序中清除,仍然继续服务...

我有一个小问题。

在我的应用程序中,服务在用户成功登录后启动。以前,如果应用程序被杀死,服务需要停止。 (例如,通过滑动从最近的应用程序列表中删除。)所以我们使用了android:stopWithTask =“true”。现在我们需要服务运行,因为它是,即使启动它的任务,从最近的应用程序列表中删除。所以我改变了服务包括android:stopWithTask =“false”。但这似乎不工作。

相关代码:

这里是与服务相关的清单部分:

android:enabled="true"

android:name=".MyService"

android:exported="false"

android:stopWithTask="false" />

在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的自定义类:

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启动。之后,我按主页按钮,所以应用程序移动到背景。现在我从最近应用程序的列表中删除应用程序。当时,我应该看到Toast和控制台消息,根据这个方法的描述:

public void onTaskRemoved (Intent rootIntent)

Added in API level 14

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.

Parameters rootIntent The original root Intent that was used to

launch the task that is being removed.

但我没有看到任何。服务在onStartCommand中返回START_STICKY,所以我认为onTaskRemoved应该与标志android:stopWithTask =“false”一起触发。

我缺少什么吗?

让我知道,如果我需要添加一些代码,这可能是重要的,弄清楚是什么问题。

P.S .:我测试了4.2.2到现在。

P.S .:我刚刚在4.1.2测试了相同的代码,服务保持运行,并且我在日志中得到消息“onTaskRemoved called”。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值