-2

Android-Oreo has essentially broken my App and left me dead in the water..

My Objective: Run in the background, and react when the "Location" setting gets turned ON.

I do not need to actually use the Location - I simply need to detect when it becomes available.
(Unfortunately, as of Android-O, I can no longer use the providers_changed intent filter to do this).

How can I go about doing this? Advice would be greatly appreciated!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Studio2bDesigns
  • 578
  • 5
  • 12
  • How is this any different than your [previous question](https://stackoverflow.com/questions/54920316/)? Why are your asking the same question again? Just because you didn't get a reply before? That is not a good reason to make a duplicate – Remy Lebeau Mar 03 '19 at 18:41
  • @RemyLebeau Absolutely - I asked a similar question, multiple days passed, and now I'm re-wording my question, since my last question (days old) got absolutely no response.. How else am I supposed to get a possible answer? I've done hours upon hours of research, and am at my wits end.. Any suggestions? or just criticism? – Studio2bDesigns Mar 03 '19 at 18:56
  • Android O did not remove the `providers_changed` broadcast, only made it so background apps can't register via app manifest to receive implicit broadcasts not specifically targeting the apps. But the [docs](https://developer.android.com/about/versions/oreo/android-8.0-changes) say this restriction can be changed in the app settings, have you tried that? The [docs](https://developer.android.com/guide/components/broadcasts) also say context-based broadcast registrations are still allowed, have you tried registering for the broadcast using `Context.registerReceiver()` instead of the app manifest? – Remy Lebeau Mar 03 '19 at 19:01
  • Update: I tried registering dynamically again, this time using the `LocationManager.PROVIDERS_CHANGED_ACTION` constant, rather than the hard-coded string `android.location.PROVIDERS_CHANGED`, and it seems to be working (for now).. This is great news.. Although I would still love to know how I might go about removing the restriction directly from the App's settings, as I assume this would be less likely to be killed.. Could you possibly point me in the right direction for how to go about requesting the user to change this? – Studio2bDesigns Mar 03 '19 at 19:38

1 Answers1

0

SOLUTION:
Register your BroadcastReceiver dynamically (from within your code), instead of from the Manifest.. Also, instead of checking for the hard-coded regex android.location.PROVIDERS_CHANGED, you should use LocationManager.PROVIDERS_CHANGED_ACTION (and of course import the LocationManager).

Example:

public void buttonClick(View view) {

    IntentFilter filter = new IntentFilter();
    filter.addAction("android.location.PROVIDERS_CHANGED");

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
                Log.i(TAG, "Action MATCHES LocationManager.PROVIDERS_CHANGED_ACTION!");
            }
        }
    };

    this.getApplicationContext().registerReceiver(receiver, filter);
    Log.i(TAG, "RECEIVER HAS BEEN REGISTERED");

}


Remember to unregister the receiver in your code appropriately, as it will indeed continue running in the background even after the user presses the back-button or home-button. The only time it will stop receiving is if the user kills it from the multitask button, or force stops it.

Studio2bDesigns
  • 578
  • 5
  • 12