【发布时间】:2023-03-13 01:20:02
【问题描述】:
在我的 Android 应用中,我通过调用从 Activity 中启动 IntentService
startService(new Intent(this, MyService.class));
它就像一个魅力。我可以在 Activies 之间移动,按 Home 按钮切换到其他应用程序……它仍然可以工作。但是,如果我从最近的屏幕中删除我的应用程序,我的服务就会停止。我怎样才能避免这种情况?换句话说,即使我的应用已从最近的应用中关闭,我如何才能让我的服务继续运行?
我的服务代码如下:
public class MyService extends IntentService {
public MyService() {
super("MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
//Here I run a loop which takes some time to finish
}
}
【问题讨论】:
-
IntentService 并非一直在运行,但它可以在 Intent 基础上运行。您使用 Intent 发送一个命令(做某项工作),然后服务执行该工作并在此之后自行停止。
标签: android android-service intentservice android-intentservice