Reputation: 7106
I am in a train and suddenly thought of something.
If I am allowed to declare my service as below:
<service
android:name=".service.MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="INTENT_SAMPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</service>
I know that doing below will result to IllegalArgumentException: Service Intent must be explicit
error.
Intent intent = new Intent("INTENT_SAMPLE");
intent.setType("text/plain");
startService(intent);
Then, what is the use of intent-filter
in this case, if there is any?
Upvotes: 2
Views: 3052
Reputation: 7106
As mentioned in the other answers, the intent filter helps your service classify the type of action to execute. So for example, you can try starting the service like below:
Intent intent = new Intent(this, MyService.class);
intent.setAction("INTENT_SAMPLE");
startService(intent);
Then in MyService
class, check for the action like so:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if("INTENT_SAMPLE".equals(intent.getAction())) {
// do INTENT_SAMPLE action here
}
else {
// do non INTENT_SAMPLE action here
}
}
Since https://developer.android.com/guide/components/intents-filters#Types does not recommend intent filters for services, drop the action from manifest. To achieve the same result as above, simply add an extra to your intent like so:
Intent intent = new Intent(this, MyService.class);
intent.putExtra("INTENT_SAMPLE", true);
startService(intent);
Then in MyService
class, check for the action like so:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent.getBooleanExtra("INTENT_SAMPLE", false)) {
// do INTENT_SAMPLE action here
}
else {
// do non INTENT_SAMPLE action here
}
}
Cheers!
Upvotes: 0
Reputation: 159
try this code....
<code>
//MainActivity.class
Intent serviceIntent = new Intent(context,MyService.class);
serviceIntent.setAction("INTENT_SAMPLE");
startService(serviceIntent);
// Myservice.class
public int onStartCommand(Intent intent, int flags, int startId)
{
string action=intent.getAction();
if(action.equals("your action"))
{...}
}
</code>
Upvotes: 2
Reputation: 256
the definition for intent is An Intent is a simple message object that is used to communicate between android components such as activities, content providers, broadcast receivers and services. Intents are also used to transfer data between activities.
and there are two types of intents Implicit Intent Explicit Intent
Implicit intent is use two define the action that you want to perform for different activities
Explicit Intent is used for launch a specific app component, such as a particular activity or service in your app
that's why you have to defined the intent in intent-filter in manifest so the system will know which type of intent is it
you can refers this links for further and detailed understanding of the topic :- https://developer.android.com/guide/components/intents-filters https://developer.android.com/guide/topics/manifest/intent-filter-element
Upvotes: 1
Reputation: 62429
If you want to use a service to perform different actions, then declaring an intent filter will help your service match against different actions you want to perform.
It allows you to use the same service for different actions, instead of creating two separate services for example.
However, having intent filters declared for a service is not regarded as a good practice, and this is what the docs had to say:
Caution: To ensure your app is secure, always use an explicit intent when starting a Service and do not declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you cannot be certain what service will respond to the intent and the user cannot see which service starts.
I found many answers on SO only regarding same:
I hope it will clear to you.
Thank you.
Upvotes: 1