How to cancel this repeating alarm?
Call cancel()
on AlarmManager
with an equivalent PendingIntent
to the one you used with setRepeating()
:
Intent intent = new Intent(this, AlarmReceive.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
cancel repeating alarm at specific time
Your code makes no sense. You have to keep in mind three basic things:
- All alarms are linked to the specific application.
- Alarms functionality is based on the pending intents.
- Cancellation can be done my matching to specific
Intent
.
Considering that, you can implement the following solution.
Create basic alarm as usual:
Intent myIntent = new Intent(this, Target.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
Create another alarm, which will be responsible for cancellation and put pendingIntent
from the 1st alarm to it:
Intent cancellationIntent = new Intent(this, CancelAlarmBroadcastReceiver.class);
cancellationIntent.putExtra("key", pendingIntent);
PendingIntent cancellationPendingIntent = PendingIntent.getBroadcast(this, 0, cancellationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, time, cancellationPendingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Where CancelAlarmBroadcastReceiver
is the following:
public class CancelAlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PendingIntent pendingIntent = intent.getParcelableExtra("key");
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pendingIntent);
}
}
I didn't check it, but I think it should work.
Android: Unable to cancel repeating alarm
I'm a noob here. But I think if you want to cancel the alarm, you have to use the exact same instance of PendingIntent. That means "their action, data, type, class, and categories are the same" as defined by filterEquals(Intent) . Therefore, you need to use the same requestCode here, not 0. Maybe you could use a list to store the Notification Ids, and iterate them to cancel all alarms.
Unable to cancel repeating alarm Android
As ci_ mentioned in the comments, it is possible that "those 100 "extra" alarms already triggered before the cancel happens". For anyone else who has the same problem, here is a solution. I tested the AlarmManager and it seems to work best if you have a delay of at least 200 ms. for a lower delay use a Handler
. Example from the question using Handler:
public class MainActivity extends Activity {
private boolean pressed = false;
private boolean done = false;
private Handler worker;
private Runnable method;
long msInterval = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
worker = new Handler();
method = getMethod();
Button toggle = (Button)(findViewById(R.id.toggle));
toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!pressed) {
worker.post(method);
pressed = true;
} else {
done = true;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
done = true;
}
private Runnable getMethod() {
return new Runnable() {
public void run() {
Log.w("running", "run");
if (done) {
Toast.makeText(getApplicationContext(), "Finished!", Toast.LENGTH_SHORT).show();
Log.w("running", "close");
} else {
worker.postDelayed(method, msInterval);
}
}
};
}
}
On first button press the handler starts the runnable, and on each call the runnable calls itself. On second button press condition done
is set to true, thus the runnable finishes after one (cleanup) round.
Related Topics
Arrayadapter in Android to Create Simple Listview
How to Create a User-Defined Function in SQLite
Adding Button Action in Custom Notification
How to Get Current Activity's Layout and Views via Adb
How to Use Web Camera in Android Emulator to Capture a Live Image
Updating Android UI Using Threads
Checkbox Unchecked When I Scroll Listview on Android
Remove App from Recent Apps Programmatically
No Generated R.Java File in My Project
Android Gridlayout with Dynamic Number of Columns Per Row
How to Place Fragment Arrylist Scrollabletabsactivity
Read Only File System on Android
Notifydatasetchange Not Working from Custom Adapter
Ffmpeg for a Android (Using Tutorial: "Ffmpeg and Android.Mk")
Change Drawable Color Programmatically
Change the Color of a Checked Menu Item in a Navigation Drawer