1
how to use setallowWhileIdle method for repeatetive task in alarmManager, 
because I think the only method that overcome doze mode, 
I need to work 24*7 task in repeatetive mode, 
is there any example please help I am new to android 

 private void scheduleAlarm() {
        try{

            Intent intent = new Intent(getApplicationContext(), KeepAliveAlarmReceiver.class);
            final PendingIntent pIntent = PendingIntent.getBroadcast(this, 
              KeepAliveAlarmReceiver.REQUEST_CODE,
                    intent, PendingIntent.FLAG_MUTABLE);
            long firstMillis = System.currentTimeMillis();// alarm is set right away
            AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP,firstMillis,60000, pIntent);
            System.out.println("enters into alarm manager");
        }catch (Exception e)
        {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

I need to do like this on setallowwhileIdle method it doesn't support repeatetive task timer.

Nns_ninteyFIve
  • 439
  • 4
  • 12

2 Answers2

0

Please read this post by @lyc001. Android - Repeating Alarms Allowed While Idle

It says

the only Apis available in AlarmManager for Android 23 are setExactAndAllowWhileIdle and setAndAllowWhileIdle which are not for repeating alarms.

You should use either of these APIs and "reschedule the alarm every time it fires". So, A alarm that registers another alarm, and so on.

martian1231
  • 15
  • 1
  • 4
0

Here is the code to make your life easier!

Register for the first time (MainActivity.java)

 AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
                                Intent intent = new Intent(getApplicationContext(), AlarmRBActivity.class);

                                boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(), 007,
                                        intent,
                                        PendingIntent.FLAG_NO_CREATE) != null);

                                if(!alarmUp){

                                    long triggerTime = intent
                                            .getLongExtra("TRIGGER_TIME", System.currentTimeMillis());

                                    triggerTime += TimeUnit.MINUTES.toMillis(2);

                                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 007, intent, 0);

                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                                        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
                                    }else if (Build.VERSION.SDK_INT >= 19) {
                                        alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
                                    } else {
                                        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
                                    }
                                    
                                    Log.e("alarm", "Alarm registered!");
                                }else{
                                    Log.e("alarm", "Alarm already registered!");
                                }

Register when triggered/ Register in loop (AlarmBRActivity.java)

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 007, AlarmIntent, 0);
            long triggerTime = intent
                    .getLongExtra("TRIGGER_TIME", System.currentTimeMillis());
            triggerTime += TimeUnit.MINUTES.toMillis(2);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
            }else if (Build.VERSION.SDK_INT >= 19) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);
            }

Source: Android AlarmManager not working on some devices when the app is closed

martian1231
  • 15
  • 1
  • 4