how to use setAndAllowWhileIdle in repeatetive task , because it avoids from doze mode , any example for this android

880 views Asked by At

How to use setallowWhileIdle method for repetitive task in AlarmManager, because I think that is the only method that overcomes doze mode. I need to work 24*7 task in repetitive 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.

2

There are 2 answers

0
martian1231 On

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.

0
martian1231 On

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