如何从 XML 启动服务?
我没有运气从首选项屏幕启动服务,并且在网上找不到任何示例。这就是我正在做的:
首选项 XML:
<PreferenceScreen
android:title="Start Service">
<intent
android:action="com.test.app.myservice" />
</PreferenceScreen>
清单:
<service
android:enabled="true"
android:name=".myservice">
<intent-filter>
<action
android:action="com.test.app.myservice" />
</intent-filter>
</service>
错误:
ERROR/AndroidRuntime(7912): android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=com.test.app.myservice }
有什么想法吗?
I'm having no luck getting a service to start from a preference screen, and can't find any examples online. This is what I'm doing:
The preference XML:
<PreferenceScreen
android:title="Start Service">
<intent
android:action="com.test.app.myservice" />
</PreferenceScreen>
The manifest:
<service
android:enabled="true"
android:name=".myservice">
<intent-filter>
<action
android:action="com.test.app.myservice" />
</intent-filter>
</service>
The error:
ERROR/AndroidRuntime(7912): android.content.ActivityNotFoundException:
No Activity found to handle Intent { act=com.test.app.myservice }
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
错误消息显示了发生的情况:您需要启动一个活动而不是一个服务。尝试构建一个正常的偏好活动并处理那里的设置更改。
The error message shows what happens: You need to start an Activity and not a Service. Try to build a normal preference activity and handle the change of the setting there.
为什么不尝试实现 registerOnSharedPreferenceChangeListener< /a> 并将其连接到复选框或其他东西。这样,您就可以以编程方式启动或停止服务。我认为这可能是处理这种情况的更优选的方法。如果您需要显示服务的当前状态(无论是选中还是取消选中),请查看绑定本地服务。
Why not try implementing a registerOnSharedPreferenceChangeListener and hook it up to a checkbox or something. That way, you can start or stop a service programmatically. I think this is probably a more preferred way to handle this situation. If you need to show the current status of the service (whether to check or uncheck) take a look at binding a local service.
您可以为您的意图注册一个接收器并从接收器启动服务,如果您愿意,可以从首选项屏幕的任何复选框启动服务,否则您可以使用现有的回调函数 onPreference 更改监听器来启动首选项服务活动..
you can register a receiver for your intent and start service from receiver, this is for if you want to.start service from any check.box of your preference screen, or else you can use existing callback functions onPreference change listners to start service in preference activity..