我有一点问题.
在我的应用程序中,在用户成功登录后启动服务.以前,如果应用程序被杀,该服务需要停止.(比如,通过滑动从最近的应用程序列表中删除.)所以我们使用过android:stopWithTask="true".现在我们需要服务按原样运行,即使启动它的任务从最近的应用程序列表中删除.所以我把服务改为包含android:stopWithTask="false".但这似乎不起作用.
相关代码:
这是与服务相关的明显部分:
<service
android:enabled="true"
android:name=".MyService"
android:exported="false"
android:stopWithTask="false" />
Run Code Online (Sandbox Code Playgroud)
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
AbstractService.java是扩展的自定义类Sevrice:
public abstract class …Run Code Online (Sandbox Code Playgroud)