Search code examples
javaandroidandroid-studio-2.0

Reset integer daily, in specific time


I need to reset an integer "daily" at a set specific time of day. In my activity, I write the value of daily into Shared preferences, then with an alarm manager and broadcast receiver try to set it to zero. everything works, besides the shared preferences and the value still remains what it was. I think that I need to import a different context for the shared prefs. Any help is welcomed.

The receiver:

 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();

}

The alarm manager:

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);

The Shared preferences in the Main Activity:

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);

Solution

  • So I made a few changes to the code and added a PendingIntent requestCode. The code looks like this

    The place where you set up your Alarm:

    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);
    }
    

    and the place where you schedule it (say onClick):

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

    the Broadcast receiver should be the same as above. And also do not forget to add the receiver to the manifest:

    <receiver android:name=".MyAppReciever"></receiver>