Android IntentService正在启动新服务-在哪个线程上?
我有一个维护意图服务,它使用AlarmManager每30秒启动一次主服务。 我使用IntentService的原因是我希望我的MainService在后台线程上运行 我的问题是-如果IntentService使用startService(new Intent(this,MainService.class))启动一个新服务;,主服务在哪个线程上启动?IntentService的线程还是UI线程 这是我的密码:提前谢谢Android IntentService正在启动新服务-在哪个线程上?,android,multithreading,intentservice,Android,Multithreading,Intentservice,我有一个维护意图服务,它使用AlarmManager每30秒启动一次主服务。 我使用IntentService的原因是我希望我的MainService在后台线程上运行 我的问题是-如果IntentService使用startService(new Intent(this,MainService.class))启动一个新服务;,主服务在哪个线程上启动?IntentService的线程还是UI线程 这是我的密码:提前谢谢 /** * A service that maintains all the
/**
* A service that maintains all the required parts of Smoove alive. In case of a
* system startup or a crash of the main service, WatchDogService restarts the
* required service
*/
public class WatchDogService extends IntentService {
// Holds the alarm manager instance.
AlarmManager alarmMgr = null;
public WatchDogService() {
super("WatchDogService");
}
@Override
protected void onHandleIntent(Intent intent) {
log.info("WatchDogService onHandleIntent");
Intent intentMainService = new Intent(this, MainService.class);
intentMainService.addFlags(Intent.FLAG_FROM_BACKGROUND);
startService(intentMainService);
}
if(!isRegisteredToAlarmManager){
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
registerToAlarmManager();
}
}
// Registers the service to the alarm manager. to start in every INTERVAK
// seconds.
private void registerToAlarmManager() {
// Build the intent.
log.info("entered registerToAlarmManager");
Intent intent = new Intent(this.getApplicationContext(),WatchDogService.class);
PendingIntent pendingIntent = PendingIntent.getService(
this.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Pull the alarm manager service to register the service.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 0,
INTERVAL * 1000, pendingIntent);
isRegisteredToAlarmManager = true;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}服务始终运行其进程的主线程。运行
startService()
的线程充其量应该是无关紧要的(最坏的情况是,它可能会产生问题,但不会使被调用的服务在该线程中运行)
(只是澄清一下:
IntentService
——它只在后台线程上调用。)如果说在新服务中的主线程中运行检查(如果是主线程)可能会更清楚一些:Looper.getMainLooper().getThread()==thread.currentThread();或打印id:Thread.currentThread().getId()