0
我正在使用Android AlarmManger來安排IntentService在小間隔後運行。Android IntentService僅執行一次
這裏是我的AlarmManger:
static void setNextSchedule(Context ctx, long interval){
AlarmManager manager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
Intent nIntent = new Intent(ctx, DService.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, 1, nIntent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + interval, interval, pIntent);
Log.i(ScheduleAlarm.class.getSimpleName(), "Next alarm set");
}
而且我IntentService:
@Override
protected void onHandleIntent(Intent intent) {
Log.i(DService.class.getSimpleName(), "Service starting");
ScheduleAlarm.setNextSchedule(getApplicationContext(), 15000);
}
目前,它只是持有的代碼進行測試。 現在,這個代碼將成功運行,一旦應用程序啓動時沒有問題,但15秒後它將聲明服務在logcat中啓動,但代碼不會執行。
如何讓intenet服務在每次運行時執行onHandleIntent
內的代碼?
感謝
「但代碼將不會執行」 - 什麼是「代碼」?你如何確定它「不會執行」?爲什麼你對一個名爲'DService'的類使用getActivity()''PendingIntent'作爲'Intent'? – CommonsWare
@CommonsWare - 你是對的,改成'getService()',現在一切正常。添加爲答案,我會接受。謝謝 – Wahtever