2013-03-03 74 views
0

工作以及在仿真器,但不這是我設置報警代碼:AlarmManager在真實設備

public void SetAlarm(Context context, int tag, long time){ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(context, Alarm.class); 
    i.putExtra("position", tag); 
    PendingIntent pi = PendingIntent.getBroadcast(context, tag, i, PendingIntent.FLAG_CANCEL_CURRENT); 
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ time, pi); // Millisec * Second * Minute 
} 

這是我onRecieve梅索德:

public void onReceive(final Context context, Intent intent){ 
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "my wak up lo"); 
    wl.acquire(); 

    position = intent.getIntExtra("position", -1);   
    new PostManager(context, position); 

    wl.release(); 
} 

這是用模擬器運作良好。同時我設置了一個鬧鐘,它會在模擬器和真實設備中24小時後觸發。該工作由模擬器完成,但不在真實的設備中。這是發生在PowerManager.FULL_WAKE_LOCK還是其他? 我已經努力嘗試,但未能找到任何解決方案。

+0

你確定''setAlarm()'方法中變量'time'的值是否正確?添加一些調試日誌記錄並檢查您的logcat,以確保該警報實際上是在您認爲應該設置的時間內設置的。 – 2013-03-04 09:12:54

+0

@DavidWasser:'時間'是正確的,因爲我同時在模擬器和真實設備中設置了警報。該警報應在4小時後觸發。在模擬器中觸發,但不是在真實的設備中。如果我將鬧鐘設置爲小於4小時,則在真實設備鬧鐘中工作良好。我認爲我的鬧鐘無法將我的設備從睡眠中喚醒。 – Shoshi 2013-03-04 09:54:10

+0

我也試過'PowerManager.PARTIAL_WAKE_LOCK'而不是'PowerManager.FULL_WAKE_LOCK',但它不起作用 – Shoshi 2013-03-04 09:55:59

回答

0

對不起,你的問題出在PostManager。在PostManager中,我正在用另一個字符串檢查會話cookie。那麼會發生什麼?這裏是答案。在4小時之前,會話cookie保留在cookiemanager上,因此.equal(another_string)的檢查工作正常。但我認爲4小時後會話cookie過期並且.equal(another_string)會拋出NullPointerException。所以我只是檢查我得到的cookie是否爲null。這解決了我的問題。再次抱歉的傢伙。