Pikaling
Pikaling

Reputation: 8312

Open System Settings from a Service

I am writing a service to collect location readings while my application is running in the foreground. In the LocationListener, I would like to use the onProviderDisabled() method to open a dialog telling the user that the location provider is disabled, and have a button on the dialog that will launch the system's location settings panel, allowing the user to enable the location provider if they choose. If this was an activity, I would launch the system settings using startActivityforResult(), but I can only use startActivity from a service.

My question is this: is there a way I can open the settings from a service, and have this new activity close and return to my application after the user changes a setting?

EDIT: What I'm trying to achieve is a Service running from the moment the application opens until it closes and collecting location readings, maintaining a best estimate of location for use in the application. If the LocationListener within the service has onProviderDisabled called, I want this to cause a dialog to open that will give the user the option to go to the System Settings and enable location providers (or cancel and carry on, although some of the application's features won't work without location). I agree that the perhaps the Service isn't the place to do the dialog/activity launch part as it is a background component with no UI, but I'm not sure where the code for this should go.

Upvotes: 1

Views: 1971

Answers (3)

Squonk
Squonk

Reputation: 48871

From the edit to your question and the comment to Sam's answer, I'd basically do a check in the Activity (or all Activities) of the app and launch the dialog and subsequent 'Settings' page from there (if the user chooses to go to Settings).

Basically, have the Service do what it needs to based on the current environment the main Activity encounters (provider disabled/enabled). If your Service will be running when there is no user front-end then have it compensate and reduce its 'duties' accordingly.

Also, in that scenario, Sam's idea of using a notification (which in turn could cause the Settings to be opened) is a good middle ground.

EDIT To explain a little further. Take something as simple as an email app. There are two aspects to this...

Firstly there's a UI - when the user opens their email app if 'the network' is disabled the user is told so with a dialog with the option to go to network settings to enable the network. Pressing BACK (from Settings) will return to the email app and it will attempt to download any new emails. If the user decides not to enable the network they can still view previously downloaded emails (similar to partial functionality in you situation).

Secondly there is a background service which periodically (every 15 mins, 30 mins, 1 hr etc) will attempt to download any new emails even if the UI is closed. If the network is disabled it will simply go to sleep (until next download time).

In theory if a user disables the network, the background email service 'could' provide a dialog or notification to say "You do realise I can't work now?"...this is kind of what you want to do BUT if the service has other things to do it can simply do those and ignore any network-related tasks. Next time the user fires up the Activity, they then get a dialog with the option to enable the network.

Does that make more sense?

Upvotes: 1

Samuel
Samuel

Reputation: 9993

just curious. service may run even if the user is not seeing the screen, or even the screen may be turned off. is it a good idea, showing a dialog from the service.

anyway, since you know how to show an activity, in the onCreate fire the intent for the settings and finish it once you get to onActivityResult.

sure this is a simple hack.

EDIT: if you are not sure where it should go. the best i can think, and even seen in some apps is the notification area.

Upvotes: 0

Dan S
Dan S

Reputation: 9189

Yes, Service has the ContextWrapper.startActivity() method which will open the desired Settings menu. The user will select what they would like to enable then touch back to return your Activity. Once back in your activity you can check LocationManager.isProviderEnabled(). Unfortunately a service cannot take an Activity result.

Upvotes: 0

Related Questions