我知道這個問題之前被問過,但我沒有得到任何答案,我想創建一個意圖的服務,始終運行一個線程,但是當我走出從應用程序我的服務停止,然後線程也停止。我需要創造一些東西來每隔幾分鐘喚醒一次服務。或者是爲了防止應用程序被殺或關閉而終止服務。如何喚醒我的意圖服務,每5分鐘
這就是我開始我的服務
Intent intent= new Intent(Intent.ACTION_SYNC,null,this,IntentServ.class);
startService(intent);
我知道這個問題之前被問過,但我沒有得到任何答案,我想創建一個意圖的服務,始終運行一個線程,但是當我走出從應用程序我的服務停止,然後線程也停止。我需要創造一些東西來每隔幾分鐘喚醒一次服務。或者是爲了防止應用程序被殺或關閉而終止服務。如何喚醒我的意圖服務,每5分鐘
這就是我開始我的服務
Intent intent= new Intent(Intent.ACTION_SYNC,null,this,IntentServ.class);
startService(intent);
爲此,您可以使用可以每1小時開始一次服務的AlarmManager
。例如:
AlarmManager mgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent notificationIntent = new Intent(context,
UpdateService.class);
PendingIntent pendingIntent=PendingIntent.getService(context, requestCode, Intent.parseIntent(), 0);
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), AlarmManager.INTERVAL_HOUR, pendingIntent);
使用正常Service
與android.app.AlarmManager
。
不需要使用WakefulBroadcastReceiver
。
AlarmManager
和PendingIntent
你在這種情況下所需要的。 按照下面的例子:
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
/* ---創建掛起的意圖,這將在執行時喚醒--- */
PendingIntent operation = getUpdatePolicyOperation();
am.set(AlarmManager.RTC, alarmTime, operation); // alarm time is millisecond time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type).
您可以在AlarmManager
閱讀更多關於報警模式在Here或在Here
希望它的幫助。
使用普通服務代替IntentService – Chandrakanth
AFAIK您可以使用一個wakefulbroadcast與一個處理程序... alarmmanager也會幫助您在這裏,,,,你試過 – Ranjit
正常服務工作一段時間,而意圖服務工作了很多時間,然後意向服務是最有用的 – Malo