Can't Stop the Ringing Alarm from Another Activity

stop alarm from ringing in another activity - android

the Ringtone r value that declared in the BroadcastReceiver is not the same as the one that you defined in your Activity, you have to do the something that make the r variable has more scope in your application:

you can defined the r variable as

static Ringtone;

in your broadcastreceiver and then assign value to it in onReceive() method, then to call that variable from your activity just declare new r variable as bellow:

Ringtone r=yourpackge.Your_BroadcastReceiver_Class.r

then call

r.stop();

cannot cancel the current alarm in Android

The problem is here, in WakeUpScreen:

alarmActivity.stopAlarm();

You are calling the stopAlarm() method of the AlarmActivity() and in this case, AlarmActivity.this is null. I can only assume that you are doing somethng like this in WakeUpScreen:

alarmActivity = new AlarmActivity();

This is an absolute no-no! You cannot instantiate Android components (Activity, Service, BroadcastReceiver, Provider) using the keyword new. Only Android can create and initialize these components, because these components need to have their Context setup by the framework before they can be used.

If you want to call a method in another Activity, then you need to ensure that that method is static. If you declare your stopAlarm() method as static, you will find that it complains about a few things (like AlarmActivity.this) which is why you will need to rewrite the method to take a Context parameter, something like this:

public void stopAlarm(Context context) {
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(this.ALARM_SERVICE);
alarmManager.cancel(alarmIntent);
}

How to stop an alarm in android

You can cancel the alarm like this:

     Intent intent = new Intent(this, Mote.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1253, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

Also, you should remove Intent.FILL_IN_DATA from your call to getBroadcast() in the code where you set the alarm.

Cancelling a single alarm when you have multiple alarms

You can do it like this,

In your Pending Intent you can pass a unique ID in place of requestCode

PendingIntent pi = PendingIntent.getBroadcast(context, unique_id, i, 0);

And to cancel you can use the same unique ID to cancel it, using the same Pending Intent.

am.cancel(pi);

For getting more information you can just use StackOverflow or Google, for now I think this answer will do for you. :)

How to stop service using AlarmManager

You can make pendingIntent global and use the same to set/stop as in

Intent intent = new Intent(G.context, MyService.class);
intent.setAction("1020");
pendingIntent = PendingIntent.getService(G.context, 1020, intent, 0);

btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
G.alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 2000, pendingIntent);

}
});

btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pendingIntent.cancel();
G.alarmManager.cancel(pendingIntent);

}
});


Related Topics



Leave a reply



Submit