【发布时间】:2017-09-20 12:07:03
【问题描述】:
创建了一个IntentService 并将for loop 放入onHandleIntent 方法中。每次我关闭应用程序(从最近的不强制关闭中删除)它都会停止。但是onDestroy 没有打电话。
我也尝试过不同的设备。我不认为这是内存不足的问题。
那么服务是否意味着仅在应用程序处于前台时才使用?
我必须在主线程的后台执行一些任务当用户关闭应用程序时,服务也接近了。
这是我的示例代码
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
for (int i = 0; i < 30; i++) {
Log.d(TAG, "onHandleIntent: " + i);
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
参考:How to keep an IntentService running even when app is closed?
Service restarted on Application Close - START_STICKY
【问题讨论】:
标签: android multithreading service intentservice android-recents