5

I want to get location every 15 sec and everything is ok, but in doze mode(deep sleep) my handler not working, is anyway? AlarmManager is good way but the time limit is 1 min is not repeated in doze mode.

@ReactMethod
public static void setAlarm()
{
  int interval = 50; // delay in secs
  AlarmManager am = (AlarmManager) reactContext.getSystemService(Context.ALARM_SERVICE);
  Intent intent = new Intent(reactContext, AlarmManagerReceiver.class);
  PendingIntent pi = PendingIntent.getBroadcast(reactContext, 0, intent, 0);
  am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval*1000 , pi);

}

Receiver :

public class AlarmManagerReceiver extends BroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {

          DoRoutine();

      }

}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Mohsen
  • 1,415
  • 1
  • 13
  • 26

1 Answers1

4

I want to get location every 15 sec and everything is ok

Not really.

First, there is no guarantee that you can get a location fix at all. GPS signals, for example, are not always available indoors or underground.

Second, there is no guarantee that you will get a fresh location fix every 15 seconds. For example, the device might not be moving.

Third, on Android 8.0+, you will not get location fixes anywhere close to that frequently, unless your app is in the foreground.

but in doze mode(deep sleep) my handler not working, is anyway?

Step #1: Hope that the user adds your app to the battery optimization whitelist

Step #2: Use a foreground service

Step #3: Request location updates to be delivered to you every 15 seconds (e.g., requestLocationUpdates() on LocationManager)

Step #4: Use a partial WakeLock to keep the device powered on (even though the screen turns off)

Step #5: Hope that users do not kill you for draining their battery so severely

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491