0

AlarmManager を使用して、ブロードキャスト レシーバーを使用して、1 分ごとに時計ウィジェットを更新します。これは、ウィジェット onEnabled でアラームを設定するために使用するコードです。

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

そして、このコードはonDisabledでそれを停止します:

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

数回 (おそらく 1 時間または 1 日) 後に時計の更新が停止するか、更新が遅れるという報告が多数あります。最も報告されているデバイスは、Galaxy S3、Galaxy S2、Galaxy Note です。

なぜこれが起こるのかわかりませんし、手がかりもありません。AlarmManager はしばらくすると動作を停止しますか、それとも私のコードに動作を妨げる例外がありますか?

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 が望ましい

更新: 最初のコードはアラーム、2 番目はサービスです。この場合、アラームは何分経過したかを判断し、対応するカウンターを持ちます。このコードは 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 に答える