1

My app has 2 different activities, let us name them A and B. I set an alarm in activity A with the time to trigger the alarm after 5 mins. I move to activity B and do some stuff there that takes less than a minute. I come back to activity A, what is happening here is that the alarm gets registered again and instead of the previous time of 5 mins, it takes 5 mins from now...For clarity Act A - register alarm - 5 mins to trigger a beep...Move to activity B, Act B - do some stuff - x mins...Move back to Act A - alarm registered again - x+5 mins to trigger a beep....

I dont want the above scenario to happen. I want to trigger the alarm at 5 mins only and not x+5? Any help please? It is really bugging me..Am on it from Thursday...

In act A

 AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Intent checkIntent = new Intent(getApplicationContext(),Alarm_class.class);
            checkIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pi = PendingIntent.getBroadcast(this, 0, checkIntent, 0);

            if(PendingIntent.getBroadcast(getBaseContext(), 0, checkIntent, PendingIntent.FLAG_NO_CREATE) != null)
            {
                Log.d("kunal", "Alarm is running");             
                if(log_days_came.equals("Three"))
                {
                     Log.d("kunal","3 days selected to zip log file.");
                    // am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 180000, pi);
                     am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 2*60000, pi);
                     Log.d("kunal","after the pending intent 3.");
                 }
    }
            else
            {
                if(log_days_came == null)
                {
                    Toast.makeText(getApplicationContext(), "configure the days to zip log files", Toast.LENGTH_LONG).show();
                    Log.d("kunal", "Alarm is not running");
                }

            }

            if(log_days_came.equals("Three"))
            {
                 Calendar calendar2 = Calendar.getInstance();
                 calendar2.setTimeInMillis(System.currentTimeMillis());
                 calendar2.add(Calendar.MINUTE, 3);   
            }

I go to act B...do some stuff...come back to act A...it calls the OnReceive of the alarm again, without the trigger expired...what am I doing wrong?

Kunal Shah
  • 489
  • 1
  • 4
  • 21
  • Could you post some of your code? Are you by any chance setting the alarm in onResume()? – ekholm Jul 30 '12 at 13:25
  • try save any boolean in shared preferences lis isAlarmSet and check it when you activity start or resume if alarm is not set then try to set the alarm. – Jaiprakash Soni Jul 30 '12 at 13:34
  • @JaiSoni Yes that has come to mind, but unfortunately the code to be written is not known by me... – Kunal Shah Jul 30 '12 at 13:43
  • Your code snippet does not show if your alarm is set within onResume. That would explain why any previous alarms are cancelled. – ekholm Jul 30 '12 at 14:03
  • @ekholm : I run this in onCreate...How do I put this in onResume? – Kunal Shah Jul 30 '12 at 14:07
  • You shouldn't. The suggestion from JaiSoni above is probably what you need in this case. – ekholm Jul 30 '12 at 14:13
  • @ekholm I do understand that. I dont really know how to break the registering of the alarm and setting the pending intent; In the sense that when an activity is called the alarm manager and pending intent gets called. How do I check if the activity is called once and do not register but have the pending intent on? – Kunal Shah Jul 30 '12 at 14:20
  • @JaiSoni I created a shared pref as you suggested. The code is like isAlarmSet(false){Do alarmmanager,intent,pendingintent stuff and close the loop at last after if(log_days_came.equals("Three")) loop...But now the alarm gets created once but does not call the onReceive of the pendingIntent... – Kunal Shah Jul 30 '12 at 15:20
  • Post your manifest. Your description of the problem is suspicious. – David Wasser Jul 30 '12 at 15:43
  • Thank you folks for all the support......And @JaiSoni thanks a lot. Your idea did help. I modified my code and it worked and also thanks to this link http://stackoverflow.com/questions/4556670/how-to-check-if-alarmmamager-already-has-an-alarm-set – Kunal Shah Jul 31 '12 at 12:18

0 Answers0