Как я могу обновить время тревоги менеджера без открытого приложения - PullRequest
0 голосов
/ 27 апреля 2020

Привет, я использую этот код для установки диспетчера аварий, он работает хорошо, но мне нужно изменить время, когда наступает последний раз (например, когда время стало 14:30, мне нужно изменить время будильника на 22:30 или 23:20, например), это мой код

  public void SetAlarm(){
    SharedPreferences preferences;

            preferences = getSharedPreferences(getString(R.string.app_name) , MODE_PRIVATE);
            String time = preferences.getString("time_4" , "14:20");

            Calendar ca = toCalendar(time);
            int hours = ca.get(Calendar.HOUR_OF_DAY);
            int min = ca.get(Calendar.MINUTE);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent intent = new Intent(this, MyReceiver.class);
            intent.putExtra("code" , 101);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, hours);
            calendar.set(Calendar.MINUTE, min);
            calendar.set(Calendar.SECOND, 0);

            alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

, и это код на приеме

int notification_id = intent.getIntExtra("code", 100);
    if (notification_id == 101){

        editor.putString("time_4" ,"14:22");
        editor.commit();
        new MainActivity().SetAlarm();
    }
...