Android repeats alarm, redefines itself on every open

I have below signal which is triggered from MainActivity when application starts. I think I have programmed it to override itself every time I start a new application. But it's hard for me to check. Does this really override itself OR if I close and reopen the application multiple times, I will have a lot of alarms?

How can I check how many posts I have created?

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 60000, pendingIntent);
    }

      

So for PendingIntent.getBroadcast(Context context, int requestCode, Intent intent, int flags)

  • requestCode

    there is? (I am assuming there is an ID set for the alarm, which you can override with a new alarm)
  • flags

    to do? (Tell the alarm what to do if the new alarm has the same ID?)
+3


source to share


2 answers


Yes, you are overriding it because every time you create an activity you are using the same request code. The request code is the second parameter of the PendingIntent.getBrodacast () method and acts as an identifier for your alarm to update later (to undo it). You can check if your alarm is set with this code:

boolean alarmUp = (PendingIntent.getBroadcast(context, 0, 
        new Intent("com.my.package.MY_UNIQUE_ACTION"), 
        PendingIntent.FLAG_NO_CREATE) != null);

      

However, you are not setting the alarm with the code you need to call:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 0 ,pendingIntent);

      



to set the alarm.

To your last question, there are several use cases for the flag:

int FLAG_CANCEL_CURRENT Flag indicating that if the described PendingIntent already exists, the current one should be canceled before creating a new one.

int FLAG_IMMUTABLE Flag indicating that the created PendingIntent should be unchanged.

int FLAG_NO_CREATE The> flag indicates that if the PendingIntent described does not already exist, then simply return null instead of creating it.

int FLAG_ONE_SHOT Flag indicating that this PendingIntent can only be used once.

int FLAG_UPDATE_CURRENT Flag indicating that if the described PendingIntent already exists, keep it, but replace it with additional data with whatever is in this new intent.

+1


source


Here's how you can set an alarm in Android at a specific time:

Intent intent = new Intent(context, AlarmService.class);
final int _id = Integer.parseInt(""+MCZManagers.timeToMillies(alarmTime));
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, MCZManagers.dateTimeToMillies(alarmDateTime), pendingIntent);

      



Once you destroy the app, the alarm will also be destroyed and will not play again.

0


source







All Articles