0

I have a repeating alarm that is started in the class StartActivity. The package is psychsoft.gps.v1 The code does not seem to catch whether the alarm is started or not.

        if ((PendingIntent.getBroadcast(con, 13336,
            new Intent("psychsoft.gps.v1"), 
            PendingIntent.FLAG_NO_CREATE) != null))
    {
        Log.i("aaa", "Alarm is already active");
        Toast.makeText(con, "alarm already active", Toast.LENGTH_SHORT).show();
    }



    Calendar calGPS = Calendar.getInstance();
    Intent intentGPS = new Intent(con, AlarmReceiver.class);
    intentGPS.putExtra("alarm_message", "gps");
    PendingIntent senderGPS = PendingIntent.getBroadcast(this, 13336, intentGPS, PendingIntent.FLAG_UPDATE_CURRENT);        
    // Get the AlarmManager service
    AlarmManager amGPS = (AlarmManager) getSystemService(ALARM_SERVICE);
    amGPS.setRepeating(AlarmManager.RTC_WAKEUP, calGPS.getTimeInMillis(),1000*60*5, senderGPS);

To test this code I run it once to schedule the alarm (installing the app). Then I run it again (by clicking the icon) to try to get the Toast, or Log output. I am getting neither. Why? I originally though the issue might be fixed by matching the request code of the PendingIntent to that of the if statement, but it did not solve the problem.

Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • Here check out this thread: http://stackoverflow.com/a/9575569 for a good example how to do what you want to achieve. Then in that example if alarmup is false, create your intent and schedule your alarm again. – logray Dec 28 '12 at 17:48

1 Answers1

0

Try creating and checking the intent like this instead:

Intent intentGPS = new Intent("psychsoft.gps.v1.MY_UNIQUE_ACTION");

and

if ((PendingIntent.getBroadcast(con, 13336,
            new Intent("psychsoft.gps.v1.MY_UNIQUE_ACTION"), 
            PendingIntent.FLAG_NO_CREATE) != null))
    {
        Log.i("aaa", "Alarm is already active");
        Toast.makeText(con, "alarm already active", Toast.LENGTH_SHORT).show();
    }
Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
  • In that case the intent would look like this: Intent intentGPS = new Intent("psychsoft.gps.v1.AlarmReceiver"); Is that correct? – Rilcon42 Dec 28 '12 at 21:46
  • To clarify my earlier comment: what is MY_UNIQUE_ACTION in your code? Is the class (AlarmReceiver) or is it something else? – Rilcon42 Jan 02 '13 at 20:27