2013-08-26 32 views
0

我嘗試在我的應用程序中設置鬧鐘,但是當電話鎖定時,活動不會像標準鬧鐘那樣啓動。我能做些什麼來決定這個問題?當我的電話鎖定時,我的鬧鐘不會啓動活動

Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.SECOND, 5); 

    Intent intent = new Intent(this, AlarmReceiverActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 
     12345, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager am = 
     (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
      pendingIntent); 
+0

1.登錄你的代碼。 2.檢查此(活動)值。 – pvllnspk

+0

閱讀下面的答案...實際發生的事情是,手機會短時間醒來處理鬧鐘管理器的onReceive方法,但隨後會立即回到睡眠狀態,因此您不會看到您的活動開始。 – JanBo

回答

2

在這種情況下,這是「正常」行爲。

爲了克服它,你需要獲得一個wake_lock到CPU。

只要警報接收器的onReceive()方法正在執行,警報管理器就會保持CPU喚醒鎖定。這可以保證手機在您完成廣播處理之前不會睡眠。一旦onReceive()返回,警報管理器就釋放這個喚醒鎖。 這意味着只要您的onReceive()方法完成,手機會在某些情況下進入休眠狀態。如果您的鬧鐘接收器名爲Context.startService(),,則可能是在請求的服務啓動之前手機會睡眠。爲防止出現這種情況,您的BroadcastReceiver和Service 需要實施單獨的喚醒鎖定策略,以確保電話繼續運行,直到服務可用。

這是來自:http://developer.android.com/reference/android/app/AlarmManager.html

類似的問題:https://groups.google.com/forum/#!topic/android-developers/RAg9LJmH1oo


這是你所需要的:http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Problem acquiring wake lock from broadcast receiver

相關問題