0

I use AlarmManager to update my clock widget at top of every minute using a broadcast receiver. This is the code I use to set the alarm in widget onEnabled:

am.setRepeating(AlarmManager.RTC, 0, 60000, PendingIntent.getBroadcast(context, 0, new Intent(context, ReceiverClockUpdate.class), 0));

and this code to stop it in onDisabled:

am.cancel(PendingIntent.getBroadcast(context, 0, new Intent(context, ReceiverClockUpdate.class), 0));

I have many reports that the clock stops updating after some times (Maybe 1 hour or even 1 day) or it updates with delays. The most reported devices are Galaxy S3, Galaxy S2 and Galaxy Note.

I don't know and have no clues why this happen. Does AlarmManager stops working after some times or there is an Exception in my code which prevents it from working.

4

1 回答 1

0

使用 RTC_WAKEUP .. 仅使用 RTC 手机睡眠时,闹钟管理器不会触发,它只会在手机唤醒时触发闹钟,打乱了 60 秒的间隔。

RTC_WAKEUP 会在触发时唤醒手机睡眠模式,不会弄乱间隔,另外,您需要使用 BOOT_COMPLETED 动作来进行闹钟,因为当手机关机时,再开机,它不会有闹钟设置。 .

我认为你需要做一些算法,因为这就是我所做的..

如果手机关机 3 分钟,然后再开机,你需要类似的东西

if(alarmtime < currenttime)
{
compute how many minutes have passed..
then make a variable of the minute that has passed.

variable = 3;
i = 0;
while(i<3)
{
update the clock one time
i++;
}

}

您需要将闹钟时间保存在存储中,最好是 SharedPref

更新:第一个代码是警报,第二个是服务。在这种情况下,警报将确定已经过去了多少分钟,然后有相应的计数器。这段代码只检查到 3 分钟,只需通过循环或其他方式添加变量

  public void onReceive(Context context, Intent intent) 
     {   
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
         wl.acquire();


         Toast.makeText(context, "A day has passed by.", Toast.LENGTH_LONG).show(); 

         wl.release();
         context.startService(new Intent(context, MySim.class));

        SharedPreferences a = context.getSharedPreferences("mPref",0);
        SharedPreferences.Editor editor = a.edit();



     }

 public void SetAlarm(Context context)
 {

     //retrieve alarms and getting current time
     SharedPreferences a = context.getSharedPreferences("mPref",0);

     long iFirst = a.getLong("first", System.currentTimeMillis()+(60*1000));
     long iSecond = a.getLong("second", System.currentTimeMillis()+(120*1000));
     long iThird = a.getLong("third", System.currentTimeMillis()+(180*1000));
     long currenttime = System.currentTimeMillis();
     SharedPreferences.Editor editor = a.edit();


     //editor passed =1 ililipat sa checkclassroom sa tunay na game
     //seting passed
     if(currenttime >= iFirst && currenttime < iSecond)
     {
         editor.putInt("passed", 1);
     }
     if(currenttime >= iSecond && currenttime < iThird)
     {
         editor.putInt("passed", 2);
         iFirst = iSecond;

     }
     if(currenttime >= iThird)
     {
         editor.putInt("passed", 3);
         iFirst = iThird;
     }
        editor.commit();




AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);



     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);


     am.setRepeating(AlarmManager.RTC,iFirst,60*1000, pi); 
 } 

这是服务:

             int passed = a.getInt("passed", 1);
            int counter = 1;
            while(counter <= passed)

            {
            //do the updating of the clock
            counter++;
            }

            editor.putLong("first", System.currentTimeMillis()+60 * 1000);
            editor.putLong("second", System.currentTimeMillis()+120 * 1000);
            editor.putLong("third", System.currentTimeMillis()+180 * 1000);
            editor.putInt("passed", 1);
            editor.commit();
        }
于 2013-01-06T18:24:39.127 回答