Search code examples
androidandroid-intentalarmmanagerandroid-pendingintent

Editing scheduled pending intends


I wrote an app to turn on/off WiFi at scheduled time choosen before. The way it works is preety simple: Choose time from timepicker, then just add it. Programmatically it gets data from timepicker and sets and alarm. I'd write down a code for my activity and broadcast receiver first, bellow this code I'll write my questions. Don't worry, I put comments inside the code to be more clear to read and understand

Wifi.class:

public class WiFi extends AppCompatActivity {
private final int TEMP_REQUEST_CODE = 1;
private AlarmManager alarmManager;
private TimePicker timePicker;
private PendingIntent pendingIntent;
private Intent intent;
private Context context;
private Calendar calendar;
Intent alarmIntent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wifi_class);
    timePicker = (TimePicker) findViewById(R.id.timePicker1);
    timePicker.setIs24HourView(true);
    setTitle("Set WiFi off/on");
    //store context in global variable
    context = getApplicationContext();
    //creates an intent that will be used in pending intent
    initializeView();
    //creates pending intent
    createAlarmIntent();
    //checks if alarm exists and sets matching text on the screen
    checkAlarmExists();

}

// I got an add button in my toolbar that saves the alarm, same as to delete 
an alarm
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.tick:
            Alarm();
            finish();
            return true;
        case R.id.dismiss:
            cancelAlarm();
        default:
            return super.onOptionsItemSelected(item);
    }
}
//this method initialize intent and stores it into variable, 
WiFiService.class extends BroadcastReceiver
//and all what it has to do is to turn wifi on/off
private void initializeView () {
    intent = new Intent(context, WifiService.class);

    //creating global pendingIntent variable
     pendingIntent = PendingIntent.getBroadcast(context,
            0,
            intent,
            0);
    //creating global alarmManager variable
     alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
}
//this method gets selected time from timepicker and calls another method to 
create an alarm
private void Alarm() {
     calendar = Calendar.getInstance();
    if (Build.VERSION.SDK_INT >= 23) {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getHour(),
                timePicker.getMinute(), 0
        );

    } else {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),
                0);
    }
    setAlarm();
}

/**
 * This method sets Alarm that will be executed even in Doze mode and will 
intent WifiServices class,
 * in the result it will turn wifi off or on
 *
 */
private PendingIntent createAlarmIntent() {
     alarmIntent = new Intent(this, WifiService.class);
    return PendingIntent.getBroadcast(this, TEMP_REQUEST_CODE,
            alarmIntent, 0);
}
private void setAlarm() {
//setting alarm
    PendingIntent intent = createAlarmIntent();
    if (Build.VERSION.SDK_INT >= 23) {
        alarmManager.setAlarmClock(new 
AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent), 
intent);
}
    else {
        alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), 
pendingIntent);
    }

    long AlarmMgs = calendar.getTimeInMillis() - 
Calendar.getInstance().getTimeInMillis();
    Toast.makeText(context, "Alarm will be executed in " + AlarmMgs / 1000 / 
60 + "min", Toast.LENGTH_SHORT).show();

}


/**
 * This method should cancel alarm that is being currently set. There is no 
 if/else statement 
 * because it always says that alarm exists (see next method)
 */
private void cancelAlarm() {

    alarmManager.cancel(createAlarmIntent());
    Toast.makeText(context, "Alarm dismissed", Toast.LENGTH_SHORT).show();
    checkAlarmExists();
}

/**
 * This method checks wrether alarm is set or not and assigns that into 
 TextView
 * Unfortunately it always says it exists
 */
private void checkAlarmExists() {

    boolean alarmExists =
            (PendingIntent.getBroadcast(context,
                    0,
                    intent,
                    PendingIntent.FLAG_NO_CREATE)
                    != null);

    TextView alarmSetter = (TextView) findViewById(R.id.alarmSet);
    if (alarmExists) {
        alarmSetter.setText("Alarm is set");

    } else {
        alarmSetter.setText("Alarm is not set yet");
    }
}
    //TODO: (1): Create more than 1 alarm without replacing one before
    //TODO: (2): Cancel one of them, not all of them
    //TODO: (3): Edit those alarms 
}

WifiService class:

public class WifiService extends BroadcastReceiver {

public static final String TAG = "WiFi";

@Override
public void onReceive(Context context, Intent intent) {
//        if 
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
{

        WifiManager wifiManager = (WifiManager) 
context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        Log.v("Wifi", "checked");
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
            Toast.makeText(context, "Turning wifi off", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "Turned off");
        } else {
            wifiManager.setWifiEnabled(true);
            Toast.makeText(context, "turning wifi on", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "turned off");
        }
//        }
    Log.v(TAG, "Works");
}

}

Okay, I left 3 TODOs in the code: 1. I want to create 1 or more alarms without replacing existing one, all I have to do is to set pending flag to update every time when I set an alarm? 2. If I create e.g 3 alarms, I want to cancel one of them, specified one. I will store those alarms in database anyway, so my way of thinking is that: create an alarm --> add it to a database --> display in a list --> if removed --> remove from the list --> disactivate 3. Editing alarms. I know how to edit items in database, but how to edit scheduled alarm? I mean to edit its time of fire. Is it all about recreating an alarm?

Could anyone answer for my questions? Thanks in advance.


Solution

  • When you set an alarm, you pass a PendingIntent to AlarmManager. The PendingIntent wraps an Intent. When you set an alarm, AlarmManager deletes any alarms it already has scheduled that match the PendingIntent. To determine if the PendingIntents match, the following are compared:

    • The requestCode in the PendingIntent.getBroadcast() call
    • The ACTION in the Intent
    • The CATEGORIES in the Intent
    • The DATA in the Intent
    • The Component (package name, class name) in the Intent

    NOTE: "extras" in the Intent are not compared

    So, if we want to reschedule an alarm, you just need to create a PendingIntent with the same requestCode, ACTION and Component as the previous one and set the alarm again. AlarmManager will remove the previous one and set the new one.

    If you want to have multiple alarms scheduled in parallel, you need to ensure that either the requestCodes, the Components or the ACTIONs are different, otherwise they will overwrite each other.