1

I have set an alarm using AlarmManager and PendingIntent. The app has text counter which is a typical timer clock that counts backwards until it reaches 0. I would like to know whether a alarm is already set so that I can update my text counter and start/stop button states accordingly if my activity is killed and restored.

Question/Problem:

  1. isAlarmActive flag value is always TRUE.
  2. Returns true even if app is uninstalled and re-installed.

Used solution from the previous question but doesnt seem to work. Any help will be great.


protected void onCreate(Bundle savedInstanceState) {
     ....
     boolean isAlarmActive = (PendingIntent.getBroadcast(MainActivity.this, 0, 
            new Intent(INTENT_ALARM_ACTION), 
            PendingIntent.FLAG_NO_CREATE) != null);

     if(isAlarmActive)
       changeButtonStatus(ButtonStatus.STOP);
     else
       changeButtonStatus(ButtonStatus.STARt);

}

private OnClickListener mBtnListener = new OnClickListener() 
    {
    @Override
    public void onClick(View v) {
        if(mButtonStatus == ButtonStatus.START){
           setAlarmOnAlarmManager();
           changeButtonStatus(ButtonStatus.STOP);
        }else{
           cancelAlarmOnAlarmManager();
           changeButtonStatus(ButtonStatus.START);
        }
    }        

public void setAlarmOnAlarmManager()
    {
        Calendar cal = Calendar.getInstance();
        long triggerTime = cal.getTimeInMillis() + mAlarmInterval;
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, mPendingIntent);
    }

public void cancelAlarmOnAlarmManager()
   {
        MainActivity.this.getSystemService(Context.ALARM_SERVICE);
        mAlarmManager.cancel(mPendingIntent);
        unregisterReceiver(mAlarmReceiver);
   }
Community
  • 1
  • 1
Akh
  • 5,961
  • 14
  • 53
  • 82
  • how and where is the variable `mPendingIntent` set? – David Wasser Aug 14 '14 at 21:11
  • Have a look: https://justanapplication.wordpress.com/tag/pendingintent-getbroadcast/. Also http://stackoverflow.com/questions/7273297/pendingintent-getbroadcast-never-returns-null. Are you sure you are not creating a matching pending intent some place else ? – Mr_and_Mrs_D Aug 15 '14 at 14:16

0 Answers0