2014-01-28 68 views
0

我正在編寫一個應用程序,並且讓我的鬧鐘BroadcastReceiver無法啓動所需的意圖。代碼如下:Android - 啓動Wackelock的鬧鐘未啓動意向服務

public void onReceive(Context context, Intent intent) 
{ 

    Log.d("Alarm:","Running WakeLock"); 

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
    wl.acquire(); 
    WakeMe.wakelock = wl; 

    Log.d("Alarm:","Running Intent (Service)"); 
    Intent i = new Intent(context, serviceCode.class); 
    context.startService(i); 

} 

public void SetAlarm(Context context) 
{ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(context, Alarm.class); 

    PendingIntent pi = PendingIntent.getService(context, 1, i, 0); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60000, pi); // Millisec * Second * Minute 
    Log.d("Alarm:", "Alarm set to run every 1 minute/s"); 
} 

當SetAlarm()被調用的代碼運行的onReceive()和通過serviceCode類運行時,它也設置報警向上並運行報警每1分鐘。我發現的問題是,當警報觸發並運行喚醒鎖時,它不會啓動serviceCode並運行。相反,它只是忽略了新的意圖。

注意:wakelock在serviceCode結束時釋放。

任何幫助將apreciated

回答

1

使用RTC_WAKEUP與服務PendingIntent,因爲你在這裏做,是不可靠的。此外,它與您的onReceive()方法無關。如果您希望BroadcastReceiver在發生警報事件時獲得控制權,則需要使用廣播PendingIntent

如果你想使用_WAKEUP報警,我強烈建議你use WakefulBroadcastReceivermy WakefulIntentService,而不是手卷自己WakeLock邏輯。

+0

謝謝你,我用你的WakefulIntentService,它似乎很好。然而,我確實需要調整AlarmReceiver,以便檢查在啓動時是否連接了電源,我希望這對您有好處。 – PaX