2013-03-03 70 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。这解决了我的问题。再次抱歉的家伙。