0

I have an alarm manager that triggers at regular interval. A button in activity_main triggers it.

It works fine. Without any problem. Alarm manager is defined in Main class. A method (activated on button click) invokes the alarm manager. Another method should stop it or cancel it.

Code:

     public class MainActivity extends Activity {
    private PendingIntent pi;
       private AlarmManager alarmManager;

    .......................................
    ......................

    public void activate(View view) {
    //this activates the alarm manager.

    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pi.getBroadcast(this, 1, i, pi.FLAG_UPDATE_CURRENT));
    }

    public void deactivate(View view) {
//this should Deactivate the alarm manager.

    alarmManager.cancel(pi);

    }

    }

However, the second method is not working. (deactivate).

When the 2nd button is clicked, the App will crash!!

Log cat gives the reason as :

Caused by: java.lang.NullPointerException.

Any idea on how to fix this? Should I create an intent again in second method? I tried it but no use.. Any suggestions or fixes for this??

Android_Noob
  • 487
  • 2
  • 6
  • 19
  • Did you have initialize the `private PendingIntent pi;` ?? and the `private AlarmManager alarmManager;` ?? – CaptainTeemo Jan 26 '15 at 02:57
  • Nope... Could you pls tell how to do that? Cuz the Start Alarm is working fine with pi.getBroadcast(this, 1, i, pi.FLAG_UPDATE_CURRENT) . A separate initialization is required? Where and how pls? – Android_Noob Jan 26 '15 at 02:59
  • Show us the constructor code of this class. – CaptainTeemo Jan 26 '15 at 03:00
  • Also take a look on this response http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm – CaptainTeemo Jan 26 '15 at 03:02
  • Extremely sorry for **Ultra Noob** question... What is a constructor code? – Android_Noob Jan 26 '15 at 03:03
  • Thanks for referring to the question. I tried it but no luck!! The app doesn't crash immediately but will crash after the 'interval' of next alarm call.. – Android_Noob Jan 26 '15 at 03:06
  • Well in that case you have an Activity, so I think that you initialized everything on your onCreate method. But read carefully the answer linked in my previous comment and I think you will find a solution. – CaptainTeemo Jan 26 '15 at 03:06
  • Thank you @CaptainTeemo... I created a new intent and sent to a different class/activity. This time the **app is not crashing** when alarm is cancelled. However, the alarm is not actually cancelled.. The previous one still triggers!!! – Android_Noob Jan 26 '15 at 03:28
  • From your code, it seems that you did not initialize the the alarmManager on `deactivate()`. So, if you go in this activity and just call the `deactivate()`, you will get NullPointerException. Maybe you can put the `alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);` at onCreate(). Also, just recreate the pending intent at deactivate() using the same request code and intent action so that in the case your exit the Activity, you still able to cancel the alarm. – Joey Chong Jan 26 '15 at 05:48

1 Answers1

0

For anyone who has similar issue...

The AlarmManager has to be initialized again in the method that your use to deactivate it. Need to Create a Pending intent as well.

Also, make sure that you create the same pending intent(name and attributes) that you used to activate or set repeating alarm. Then, cancel it. All credits to @joey chong

Hope it helps..

Android_Noob
  • 487
  • 2
  • 6
  • 19