从服务打开系统设置

发布于 2024-11-27 18:58:14 字数 646 浏览 2 评论 0原文

我正在编写一项服务来收集位置读数,而我的应用程序在前台运行。在 LocationListener 中,我想使用 onProviderDisabled() 方法打开一个对话框,告诉用户位置提供程序已禁用,并在对话框上有一个按钮将启动系统的位置设置面板,允许用户选择启用位置提供程序。如果这是一个活动,我将使用 startActivityforResult() 启动系统设置,但我只能从服务中使用 startActivity

我的问题是:有没有办法可以从服务中打开设置,并在用户更改设置后关闭此新活动并返回到我的应用程序?

编辑:我想要实现的是从应用程序打开到关闭的那一刻运行的服务并收集位置读数,维护对位置的最佳估计以供使用该应用程序。如果服务中的 LocationListener 已调用 onProviderDisabled,我希望这会打开一个对话框,让用户可以选择转到系统设置并启用位置提供程序(或者取消并继续,尽管应用程序的某些功能在没有位置的情况下将无法工作)。我同意,Service 可能不是执行对话框/活动启动部分的地方,因为它是一个没有 UI 的后台组件,但我不确定该代码应该放在哪里。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

才能让你更想念 2024-12-04 18:58:14

从对您的问题的编辑和对 Sam 答案的评论,我基本上会检查应用程序的活动(或所有活动),然后启动对话框和随后的“设置”页面(如果用户选择去到设置)。

基本上,让服务根据主活动遇到的当前环境(提供程序禁用/启用)执行其需要的操作。如果您的服务将在没有用户前端的情况下运行,那么让它进行补偿并相应地减少其“职责”。

此外,在这种情况下,Sam 使用通知的想法(这反过来可能导致打开“设置”)是一个很好的中间立场。

编辑进一步解释一下。以电子邮件应用程序这样简单的东西为例。这有两个方面......

首先有一个用户界面 - 当用户打开电子邮件应用程序时,如果“网络”被禁用,则会出现一个对话框,其中包含转到网络设置以启用网络的选项。按“返回”(从“设置”)将返回电子邮件应用程序,并将尝试下载任何新电子邮件。如果用户决定不启用网络,他们仍然可以查看以前下载的电子邮件(类似于您情况中的部分功能)。

其次,有一个后台服务,即使 UI 关闭,也会定期(每 15 分钟、30 分钟、1 小时等)尝试下载任何新电子邮件。如果网络被禁用,它将简单地进入睡眠状态(直到下一次下载时间)。

理论上,如果用户禁用网络,后台电子邮件服务“可以”提供一个对话框或通知说“您确实意识到我现在无法工作吗?”...这正是您想要做的,但是如果服务还有其他事情要做,它可以简单地执行这些操作并忽略任何与网络相关的任务。下次用户启动活动时,他们会看到一个对话框,其中包含启用网络的选项。

这更有意义吗?

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?

风向决定发型 2024-12-04 18:58:14

是的,服务具有 ContextWrapper.startActivity( ) 方法,这将打开所需的设置菜单。用户将选择他们想要启用的内容,然后触摸返回以返回您的活动。回到您的活动后,您可以检查 LocationManager .isProviderEnabled()。不幸的是,服务无法获取活动结果。

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.

芯好空 2024-12-04 18:58:14

只是好奇。即使用户看不到屏幕,服务也可能运行,甚至屏幕可能被关闭。显示来自服务的对话框是个好主意吗?

无论如何,既然您知道如何显示活动,那么在 onCreate 中会触发设置的意图,并在到达 onActivityResult 后完成它。

当然这是一个简单的黑客攻击。

编辑:如果您不确定它应该去哪里。我能想到的最好的,甚至在某些应用程序中看到的最好的就是通知区域。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文