2016-07-16 5 views
0

私のアプリでアラームマネージャーを使用していて、午前2時にDefaultSharedPreferencesの値をリセット(0に設定)する必要があります。 私はアプリが開いているときに呼び出され、私の主な活動の機能があります。Android AlarmManagerが正常に動作しない

protected void setUpAlarmManager(){ //To delete the data of each day and pass it to Records database 
     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(this, AlarmReceiver.class); 
     PendingIntent pIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 02); 
     calendar.set(Calendar.MINUTE, 00); 
     calendar.set(Calendar.SECOND, 00); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pIntent); //set repeating every 24 hours 
     Log.i("Set", "Alarm set"); 
    } 

をそして私は、値をリセットするためにBroadcastReceiverを使用しています。

package com.javierd.iifym; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.util.Log; 

import com.javierd.iifym.sqlite.RecordsDataSource; 

//To delete the data of each day and pass it to Records database 
public class AlarmReceiver extends android.content.BroadcastReceiver { 

    private RecordsDataSource dataSource; 
    private Utils utils; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("Time", "TIMEEEE!!!!"); 
     SharedPreferences macros = context.getSharedPreferences(context.getPackageName() + "_preferences", Context.MODE_PRIVATE); //As we store data using DefaultSharedPreferences, not SharedPreferences 
     SharedPreferences.Editor editor = macros.edit(); 
     editor.putString("eaten_carbs", "0"); 
     editor.putString("eaten_proteins", "0"); 
     editor.putString("eaten_fats", "0"); 
     editor.apply(); 
    } 
} 

受信機が動作すると、AlarmManagerがランダムに動作するという問題があります。それはちょうど午前2時になっているはずですが、多分16時にアプリを入力し、値を保存して、次回17時に入力すると値が削除されるか、そうでないかもしれない私はそれを理解していない。

+0

を試してみてくださいでした。 – natario

答えて

1

このような効果は、AlarmManager.setInexectRepeating(int, long, long, PendingIntent)を使用することによって発生すると私は考えている。要求された時間の前にはできませんdocumentation

あなたのアラームの最初のトリガによると が、それはその時間の後にはほぼ完全な間隔で発生しない可能性があります 。

この方法を使用すると、Androidは複数の不正確な繰り返しアラームを同期させ、同時に発生させます。また、間隔が1日であることを考慮すると、アラームは17:30に発火する可能性があります。

アラームが午前2時に正確に発生する必要がある場合は、代わりにAlarmManager.setRepeating(int, long, long, PendingIntent)メソッドを使用することをおすすめします。

電池の消耗を減らすため、できるだけ正確なアラームを使用しないことをお勧めします。

更新されました。

トリガー時間が過去でないかどうかを確認することをお勧めします。アラームは、再起動時に消える -

あなたは電話がオフになっている場合、これは動作しません

protected void setUpAlarmManager(){ //To delete the data of each day and pass it to Records database 
     AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
     Intent intent = new Intent(this, AlarmReceiver.class); 
     PendingIntent pIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 2); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     if (Calendar.getInstance().after(calendar)) { 
      calendar.add(Calendar.DAY_OF_MONTH, 1); 
     } 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pIntent); //set repeating every 24 hours 
     Log.i("Set", "Alarm set"); 
    } 
+0

ええ、私はそれを知っていた、なぜ私はsetInexactRepeatingを使用しています。 私はsetRepeatingに変更しましたが、それでも失敗します。 アプリケーションが開かれるたびにalarmManagerが設定されるため、エラーが生成される可能性がありますか? – Javierd98

+0

@ Javierd98アプリが開かれるたびにアラームが設定されている場合は、その理由が考えられます。 'calendar.set(Calendar.HOUR_OF_DAY、02);という行は、結果の日付が将来になることを保証するものではありません。たとえば、現在の時刻が17:30の場合は、結果の時刻が02:00になります。*同じ日*、これは過去の時刻です。そのようなアラームを設定した後、システムはできるだけ早く発射しようとします。 – Dmitriy

+0

ええと、alarmManagerをどのように設定すればいいですか? – Javierd98

関連する問題