1

I had created alarm manager which invoke after 2 minutes which I want. But currently, i'm not able to set it more than 20 seconds. if I set for 20 seconds it returns output but if I set it for more than 20 I'm not able to set. I want to set it for 2 minutes. kindly help me. I had added alarm fire code

AlarmManager manager = (AlarmManager) ContextGetter.getContext().getSystemService(Context.ALARM_SERVICE);
        Intent intnt = new Intent(ContextGetter.getContext(), AlarmBroadcastReceiver.class);
        intnt.setAction("com.demo.alarmEvent");

        PendingIntent pending = PendingIntent.getBroadcast(ContextGetter.getContext(), 0, intnt, 0);

        manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ 30*1000, pending);

kindly help me i want it set it only once.

I also tried with this one.

AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                                                                Intent intnt = new Intent(EnterSysNumber.getInstance(), AlarmReceiver.class);
                                                                intnt.setAction("com.demo.Enter_number");
                                                                PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
                                                                manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
Madhav_nimavat
  • 401
  • 5
  • 19

1 Answers1

0

In Android N you'll run into Doze mode. Doze is a deep sleep mode to save battery power. It will prevent your service to preserve battery power.

AlarmManager and using setExactAndAllowWhileIdle when in low-power idle modes this duration may be significantly longer, such as 15 minutes.. But this is NOT suggested for any Play Store app, you will kill your user's battery.

  • But it work well when i set for 20 seconds but when i set it for more than 20 it not work. please help me i need it as fast as i can. – Madhav_nimavat Jan 10 '17 at 06:15
  • @ravi_koriya Use `setExactAndAllowWhileIdle` for more info check (https://developer.android.com/reference/android/app/AlarmManager.html) – Charitha Ratnayake Jan 10 '17 at 06:22
  • My min API is 18 and target API is 23 – Madhav_nimavat Jan 10 '17 at 06:30
  • Then why don't you use `Timer.scheduleAtFixedRate(TimerTask task, long delay, long period)` (Timer.https://developer.android.com/reference/java/util/Timer.html) _But this is not working 24/7_ – Charitha Ratnayake Jan 10 '17 at 06:32
  • I have 4 different broadcast receiver in my project so, is there any problem with it ? there all are for different usage. – Madhav_nimavat Jan 10 '17 at 06:36
  • And i have 1 progressbar which visible for 2 minute if between that time no output receive then it stop and invisible. – Madhav_nimavat Jan 10 '17 at 06:39
  • If you want to show a `ProgressBar ` until process has complete then you can show a transparent dialog with progress bar within your activity. When the task has completed or time out you can dismiss that dialog. – Charitha Ratnayake Jan 10 '17 at 06:47
  • but it visible when app is running but when user close app. it not visible also if operation not complete within 2 minute then automatically close. – Madhav_nimavat Jan 10 '17 at 06:50
  • Yes, you can send a Event to your activity to show the dialog which user cannot cancellable. And all the activities in your application need to extend with 'BaseActivity'. All events and ui implementation of dialog should be implement in 'BaseActivity'. For parse events you can use `EventBus` library. Which you can register your activities and unregister your activity. – Charitha Ratnayake Jan 10 '17 at 06:56
  • can u tell me how can i use event but for 2 minute time – Madhav_nimavat Jan 10 '17 at 07:24
  • First you need to resister your activity in `onCreate` method: `EventBus.getDefault().register(ActivityName.this);`, then while your start service sent Event to your Activity `EventBus.getDefault().post(new ExampleObject());`. In your activity `public void onEvent(ExampleObject event) {}` this listen all events send via `ExampleObject` from `EventBus` – Charitha Ratnayake Jan 10 '17 at 07:34