2

I have a location application that runs in background. To manage all this, I use periodic alarms to activate the localization process. The problem is that since the last update of SAMSUNG has stopped working on these devices. When the device is locked those alarms stop working. Just by activating the screen again, everything works correctly. I have tried several methods for managing alarms and the last tests have been done with the setExactAndAllowWhileIdle method

https://developer.android.com/reference/android/app/AlarmManager.html#setExactAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)

But the result has been the same. This behavior happens to me on Samsung devices with Android 8.0.

Any help or suggestion will be welcome.

Thank you in advance

1 Answers1

2

Use Broadcast receiver to get location in version 8.0...i have test it was working for me

 private PendingIntent getPendingIntent() {
        // Note: for apps targeting API level 25 ("Nougat") or lower, either
        // PendingIntent.getService() or PendingIntent.getBroadcast() may be used when requesting
        // location updates. For apps targeting API level O, only
        // PendingIntent.getBroadcast() should be used. This is due to the limits placed on services
        // started in the background in "O".

        // TODO(developer): uncomment to use PendingIntent.getService().
//        Intent intent = new Intent(this, LocationUpdatesIntentService.class);
//        intent.setAction(LocationUpdatesIntentService.ACTION_PROCESS_UPDATES);
//        return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intent = new Intent(this, LocationUpdatesBroadcastReceiver.class);
        intent.setAction(LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES);
        return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

Reference : https://github.com/googlesamples/android-play-location/blob/master/LocationUpdatesPendingIntent/app/src/main/java/com/google/android/gms/location/sample/locationupdatespendingintent/MainActivity.java

Note: Some of device is not working like OPPO,Vivo, due to security reason.. In this case you have to go to Security permissions -> autostart -> then enable autostart from there.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • Location will get if gps enabled...here is the my app you can check it https://play.google.com/store/apps/details?id=kc.care.task – Gowthaman M Oct 16 '18 at 10:47
  • 2
    In Android 8 getService() will not work above code will work – kuber singh Oct 16 '18 at 10:47
  • 2
    @kubersingh yes you are correct.. `return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);` it will not work for 8.0 so commented...for 8.0 you have to use broadcast recevier... – Gowthaman M Oct 16 '18 at 10:49