How to edit/reset Alarm Manager?
Asked Answered
C

2

7

I've set up a Preference Screen in which i have a list preference which allows user to select time interval to notify them.

They can choose whether to notify them after every 2,4,8,10 or 12 hours.

Here's my list preference:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
            android:title="Notification Timer"
            android:summary="Select when to Notify"
            android:dialogTitle="Show Notification after every:"
            android:positiveButtonText="OK"
            android:negativeButtonText="Cancel"
            android:entries="@array/entries"
            android:entryValues="@array/entries"
            android:key="listPrefs" />
</PreferenceScreen>

Here's my Settings.class file:

public class Settings extends PreferenceActivity implements OnPreferenceChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        ListPreference listPreference = (ListPreference) findPreference("listPrefs");
        listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                return false;
            }
        });
        @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        return false;
    }
}

I want to update or reset Alarms every time the user changes the time interval of notifying him/her. for ex- If user selects 4 hours than he/she must be notified after 4 hours or if user chooses 10 hours than he/she must be notified after 10 hours!

Chapfallen answered 29/7, 2014 at 6:13 Comment(2)
See the AlarmManager documentation. If you are having a specific issue with the APIs or a crash, please provide more information.Abraham
guys please write the reason for down voteTasty
T
5

Follow these steps:

  1. Copy the following methods to your Setting.java file:

    private final int NOTIFICATION_TIMER = 11;
    
    public void setAlarm(Context mContext,int requestCode,long time) {
    
        Intent myIntent = new Intent(mContext, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode, myIntent,0);
    
        cancelAlarmIfExists(mContext,requestCode,myIntent);
    
        AlarmManager alarmManager = (AlarmManager)mContext.getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis()+time), time, pendingIntent);
    }
    
    public void cancelAlarmIfExists(Context mContext,int requestCode,Intent intent){
        try {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, requestCode, intent,0);
            AlarmManager am=(AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
            am.cancel(pendingIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
  2. Call them from the onPreferenceChange() method. Like so:

    listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
    
            long time = 34352355253; // calculate millisecons from change value by user.
            setAlarm(Settings.this,NOTIFICATION_TIMER,time);
            return false;
        }
    });
    
Tasty answered 29/7, 2014 at 6:25 Comment(3)
Thanks for this anwer Bro, but it's showing an error here: setAlarm(this,NOTIFICATION_TIMER,time); The Error is: The method setAlarm(Context, int, long) in the type Settings is not applicable for the arguments (new Preference.OnPreferenceChangeListener(){}, int, long)Chapfallen
dude it is just static value. you have to calculate that value when user select it from list. If user clicks on 2 hours. You need to calculate milliseconds for 2 hours and assign it ti time variable.Tasty
please read the comment along with it. And try something with your own.Tasty
I
2

FLAG_CANCEL_CURRENT has the same effect as above answer

PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intyre answered 27/10, 2015 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.