cannot cancel the current alarm in Android
Asked Answered
H

1

1

I am new to Android. Here, I am not getting any errors ,While debugging the stopAlarm() method debugger crossed all the lines but the AlarmReceiver is not get called .

can anyone help me to fix it .

Update :

AlarmActivity.java

public void stopAlarm(Context context) {
        Intent intent = new Intent(context,AlarmReceiver.class);
        intent.setAction("ALARM_OFF");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, mAlarmId, intent,0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
    }
Hubbell answered 18/7, 2018 at 15:6 Comment(8)
Possible duplicate of What is a NullPointerException, and how do I fix it?Frug
can you help me to fix this issue ,I can't understand how my issue is same to the post you mentioned @FrugHubbell
It’s funny how every post that has NullPointerException in it gets flagged a duplicate of the post Raj linked.Denominationalism
How are you setting alarmActivity in WakeupScreen? Add the code to your original post (edit it please).Modestine
@LAD This is exactly the type of situation where declaring this a duplicate of the cononical "What is a NullPointerException" is not helpful. OP isn't calling any method on a null variable. The Android framework is doing it. And the OP has no clue why or where the framework is doing this and how he can fix it. OP is hopelessly lost unless he reads the Android source code for 2 years or gets someone else to help. Closing this as a duplicate is unhelpful at best and rude at worst. See meta.#255562Modestine
@David Wasser Yeah, exactly right. It is so common, though. Too often posts get flagged as duplicate but the linked post doesn’t exactly explain the unique situation, it is just a general solution.Denominationalism
@LAD This is unfortunately a problem because the moderators are overloaded with flags. If someone marks it as "duplicate", the moderators need to decide what to do, and often they don't have time to dig through the post or they aren't familiar enough with the programming language or the particular framework or library. I try to help as much as I can by going through Android questions that are flagged as "duplicate", but I probably am not making a dent in the backlog. I have a day job and company to run and kids and dogs waiting at home :-(Modestine
#56755669 anyone can help me in this qn @DavidWasser – ZhuHanford
M
3

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);
}
Modestine answered 18/7, 2018 at 16:29 Comment(6)
Bro ,Now the debugger crossed all the lines in the stopAlarm() method but AlarmReceiver class is not get called ,please help me to fix it bro ,I have updated the code I have currently working with @DavidWasserHubbell
Remover the 'setAction()' call. You don't need that and it causes the 'cancel()' call to failModestine
Here I am using the service file based on the Action values that's why I am using ACTION ,Please see my updated service file . @DavidWasserHubbell
Also I tried intent.putExtra() instead of setAction that also not worked @DavidWasserHubbell
I have no idea what you mean by "the debugger crossed all the lines". Anyway, In your original question you were complaining about a NullPointerException, which I explained with my answer. Now you've got something completely different. I understood that you wanted to cancel a previously set alarm. I explained how to do that. Now what you are saying is that you want to call your BroadcastReceiver to stop some media playing. For that you don't need the AlarmManager at all. Just create your Intent and then call sendBroadcast(intent). That will call your BroadcastReceiver.onReceive()Modestine
can you please help me to solve this #51542599 @DavidWasserHubbell

© 2022 - 2024 — McMap. All rights reserved.