AndroDev
AndroDev

Reputation: 3284

Service action does not work when defined in string.xml

I am facing one weired problem. I have defined my service action in string.xml and using the same in manifest like this:

<service android:name=".MyService" >
    <intent-filter >
        <action android:name="@string/my_service_action" />
    </intent-filter>
</service>

Also while starting the service I am starting like this:

Intent serviceIntent = new Intent(getResources().getString(R.string.my_service_action));
startService(serviceIntent);

Can anyone tell me where is the problem.

If I hard code the same value of action, it works perfectly fine. After some hit n try I found that it doesn't work only when used in manifest file(e.g. hard code the action value in java code only not manifest).

Upvotes: 1

Views: 1182

Answers (1)

waqaslam
waqaslam

Reputation: 68187

I dont think you can use string reference as action string for service. It must a concrete string value. As per javadoc:

android:name The name of an action that is handled, using the Java-style naming convention. [string]

For example:

<service android:name=".MyService" >
    <intent-filter >
        <action android:name="com.demo.service.MY_SERVICE" />
    </intent-filter>
</service>

and then you should use it as:

Intent serviceIntent = new Intent("com.demo.service.MY_SERVICE");
startService(serviceIntent);

Upvotes: 2

Related Questions