Cannot Cancel The Current Alarm In Android
Solution 1:
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:
publicvoidstopAlarm(Context context) {
Intentintent=newIntent(context, AlarmReceiver.class);
PendingIntentalarmIntent= PendingIntent.getBroadcast(context, mAlarmId, intent, 0);
AlarmManageralarmManager= (AlarmManager) context.getSystemService(this.ALARM_SERVICE);
alarmManager.cancel(alarmIntent);
}
Post a Comment for "Cannot Cancel The Current Alarm In Android"