开发者

How to start a service from XML?

开发者 https://www.devze.com 2023-02-07 15:30 出处:网络
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:

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">开发者_StackOverflow中文版
    <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?


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.


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.


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..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号