2016-10-09 8 views
0

設定した特定の時刻に「毎日」整数をリセットする必要があります。私の活動では、毎日の価値をShared環境設定に書き込んだ後、アラームマネージャーとブロードキャスト受信機でゼロに設定しようとします。共有された環境設定のほかにすべてが機能し、価値はまだそれのままです。私は、共有された環境のために別のコンテキストをインポートする必要があると思います。どんな助けも歓迎されます。整数を特定の時刻に毎日リセットする

受信機:

public void onReceive(Context context, Intent intent) { 

    final SharedPreferences shared = context.getSharedPreferences("Mydata", MODE_PRIVATE); 
    final SharedPreferences.Editor editor = shared.edit(); 
    int sum = shared.getInt("data1", 0); 
    Calendar c = Calendar.getInstance(); 
    String date = String.valueOf(c.get(Calendar.DATE)); 

    mainData.insert_data(date, sum); 
    int zero = 0; 
    editor.putInt("data1", zero); 
    editor.apply(); 

} 

アラームマネージャ:

Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 22); 
     calendar.set(Calendar.MINUTE, 45); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.MILLISECOND, 0); 
     final Context context = this.getContext(); 
     PendingIntent pi = PendingIntent.getService(context, 0, 
       new Intent(context, MyAppReciever.class),PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       AlarmManager.INTERVAL_DAY, pi); 

主な活動で共有好み:

final SharedPreferences shared = this.getContext().getSharedPreferences("Mydata", MODE_PRIVATE); 
     final SharedPreferences.Editor editor = shared.edit(); 
     editor.putInt("data1", 0); 
     editor.putInt("month", 0); 
     final int today212 = shared.getInt("data1", 0); 
     final int mesec212 = shared.getInt("month", 0); 
     final int limit212 = shared.getInt("budget", 0); 
+0

あなたは '' BroadcastReceiver'でSharedPreferences'を更新しますか? – mklimek

+0

はい、私はそれが問題だと思っています –

+0

私はそうは思わない。 'Integer'が更新されていないことをどうやって確認しますか? – mklimek

答えて

0

だから私は、コードにいくつかの変更を行い、 PendingIntent requestCodeが追加されました。

public void scheduleTestAlarmReceiver(Context context) { 
    Intent receiverIntent = new Intent(context, MyAppReciever.class); 
    PendingIntent sender = PendingIntent.getBroadcast(context, 123456789, receiverIntent, 0); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 23); 
    calendar.set(Calendar.MINUTE, 58); 

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
      AlarmManager.INTERVAL_DAY, sender); 
} 

、あなたはそれをスケジュールする場所(onClickの発言): コードは、この

あなたのアラームを設定する場所のように見えます

Context cn= this.getContext(); 
     scheduleTestAlarmReceiver(cn); 

放送受信機はする必要があります上記と同じです。 もマニフェストに受信機を追加することを忘れないでください:

<receiver android:name=".MyAppReciever"></receiver> 
関連する問題