我有一个 IntentService 有自己的流程,就像这样:
AndroidManifest.xml
<service
android:name="com.app.services.UpdateDatabaseService"
android:process=":updateDatabaseService"
android:label="@string/service_name"
android:exported="false" >
</service>
然后当 IntentService 运行时,它会创建两个线程。一个线程的任务之一是长任务(超过 1 分钟)。我遇到了一个 ANR 问题。
10-24 18:51:27.923: E/ActivityManager(148): ANR in com.app:FooService
10-24 18:51:27.923: E/ActivityManager(148): Reason: Executing service com.app/.services.FooService
10-24 18:51:27.923: E/ActivityManager(148): Load: 1.08 / 0.48 / 0.21
10-24 18:51:27.923: E/ActivityManager(148): CPU usage from 13433ms to 1122ms ago:
10-24 18:51:27.923: E/ActivityManager(148): 92% 1375/com.emanga:updateMangaDatabase: 87% user + 5.7% kernel / faults: 5740 minor
10-24 18:51:27.923: E/ActivityManager(148): 0.4% 148/system_server: 0.2% user + 0.1% kernel / faults: 13 minor
10-24 18:51:27.923: E/ActivityManager(148): 0.2% 45/adbd: 0% user + 0.2% kernel
10-24 18:51:27.923: E/ActivityManager(148): 0.1% 758/logcat: 0% user + 0.1% kernel
10-24 18:51:27.923: E/ActivityManager(148): 0% 266/com.android.phone: 0% user + 0% kernel / faults: 2 minor
10-24 18:51:27.923: E/ActivityManager(148): 93% TOTAL: 87% user + 6.2% kernel + 0% irq
10-24 18:51:27.923: E/ActivityManager(148): CPU usage from 1804ms to 2442ms later:
10-24 18:51:27.923: E/ActivityManager(148): 70% 1375/com.emanga:updateMangaDatabase: 65% user + 4.9% kernel / faults: 123 minor
用户界面绝不会被阻止。因此,如果服务位于单独的进程(不是 UI 线程)中,为什么会出现此错误?
编辑1:
我将 IntentService 更改为 Service,但我遇到了同样的问题。我会尝试解释我想要得到什么。我的应用程序的架构具有显示使用加载器从数据库中恢复的数据的活动。如果数据库没有请求的数据,那么它将请求一个将从互联网恢复数据的服务(该服务管理互联网请求,它解析一些 html,用新数据更新数据库,最后服务通知更改)
该服务有自己的进程,因为尽管该服务会捕获可能发生的异常,但这样应用程序对 Internet 或解析器故障的抵抗力更强(应用程序不会崩溃)
Loaders 的目的是从数据库中获取数据,而服务的目的是从 Internet 中获取数据并更新数据库。Loaders 和 Services 之间的通信仅用于通知更改或请求。
最后,这个服务有一个任务队列和一个运行任务的执行器。
public class UpdateDatabaseService extends Service {
private static final String ACTION = "com.app.services.UpdateDatabaseService";
public static final String ACTION_TASK_1 = ACTION + ".latestChapters";
public static final String ACTION_TASK_2 = ACTION + ".latestMangas";
private static final byte PARALLELTASKS = 2;
public LinkedBlockingQueue<Runnable> tasks = new LinkedBlockingQueue<Runnable>();
private ExecutorService executor = Executors.newFixedThreadPool(PARALLELTASKS);
private final IBinder mBinder = new MyBinder();
public class MyBinder extends Binder {
public UpdateDatabaseService getService() {
return UpdateDatabaseService.this;
}
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
@Override
public void onCreate(){
// By default always it does DefaultTask1
tasks.put(new DefaultTask1());
if(!isEmptyTable("tableFoo")){
tasks.put(new DefaultTask2());
}
executor();
}
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent != null && intent.getAction() != null) {
String action = intent.getAction();
if(action == ACTION_TASK_1){
tasks.put(new Task1());
} else
if(action == ACTION_TASK_2){
tasks.put(new Task2());
}
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
private void executor(){
while(true){
executor.execute(tasks.take());
}
}