I have a few intents that activity sends to service. All of those are registered in manifest:
<service android:name=".location.LocationService" android:label="@string/location_service_started">
<intent-filter>
<action android:name="@string/location_service_set" />
<action android:name="@string/location_service_start" />
<action android:name="@string/location_service_stop" />
</intent-filter>
</service>
But only location_service_start and location_service_stop intents are received. What could be the reason? There is my receiver code:
private BroadcastReceiver LocationServiceReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(getString(R.string.location_service_stop)))
{
showMessage("stop");
}
if(intent.getAction().equals(getString(R.string.location_service_start)))
{
showMessage("start");
}
if(intent.getAction().equals(getString(R.string.location_service_set)))
{
showAlertBox("set");
}
}
};
So I never see "set" message. I've even tried put sendBroadcast for "start" and "set" messages in the same place, but everything still the same. "start" - OK, "set" - never received.
Functions that fires intents:
protected void start()
{
Intent intent = new Intent(getString(R.string.location_service_start));
getApplicationContext().sendBroadcast(intent);
开发者_运维知识库 }
protected void set(double lat, double lon, double rad)
{
Intent intent = new Intent(getString(R.string.location_service_set));
intent.putExtra("lat", lat);
intent.putExtra("lon", lon);
intent.putExtra("rad", rad);
getApplicationContext().sendBroadcast(intent);
}
Both are correct send, without errors, actions are correct.
UPD:
Oh, my fault. I forgot to add filter.addAction... for new intent. I'm sorry. But answers was really useful! Thank you!
All of those are registered in manifest:
Generally, you do not use string resources for action strings in an <intent-filter>
, because you never want to internationalize them.
Generally, you do not use an <intent-filter>
at all with a service unless you are exposing that service to third-party apps. In fact, right now, you are exposing your service to third-party apps, so anyone can send these commands to your service.
But only location_service_start and location_service_stop intents are received
No, none of them are received by the service. You are sending broadcasts in the Java code. Services do not receive broadcasts.
Functions that fires intents:
Do not use getApplicationContext()
unless you know what you are doing. Whatever you are calling getApplicationContext()
on is a Context
, so you can just call sendBroadcast()
on it.
Copy & Paste from this question I just answered. Should be the same issue.
You have to put each <action />
tag inside a seperate <intent-filter />
tag in your manifest.
This should be a bug, since the doc states you can put more than one action inside a filter tag:
Zero or more action [..] tags should be included inside to describe the contents of the filter.
Source
精彩评论