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.

4

1 回答 1

0

尝试像这样创建和检查意图:

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

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();
    }
于 2012-12-28T17:51:01.223 回答