Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of Service Intent-Filter inside Manifest.xml

Tags:

from android developers : "Components(service) advertise their capabilities — the kinds of intents they can respond to — through intent filters.

I just cant understand the purpose of intent filter inside service in the Manifest.xml, what is the capability here?

<service     android:name="com.x.y"     android:enabled="true"     android:exported="true" >     <intent-filter>         <action android:name="com.x.y" />     </intent-filter> </service> 

and what's he difference if i remove the intent-filter?

 <service        android:name="com.x.y"  </service> 

thanks.

like image 998
joseRo Avatar asked Jul 30 '13 07:07

joseRo


People also ask

What is the purpose of intent filter?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.

What is the significance of intent filter element in manifest file?

An intent filter is an expression in an app's manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.

What's the difference between intent and intent filters?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.

What is the specific role of intent receiver and intent filter in broadcast receiver class?

An intent-filter determines which action should be received. To create a BroadcastReceiver, you have to extend the BroadcastReceiver class and override onReceive(Context,Intent) method. Here you can check the incoming intent with Intent. getAction() and execute code accordingly.


2 Answers

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.

The example will explain better.
Suppose you have following declaration in manifest file:

<service     android:name="MyService" >     <intent-filter>         <action android:name="com.x.y.DOWNLOAD_DATA" />         <action android:name="com.x.y.UPLOAD_DATA" />     </intent-filter> </service> 

Then in your IntentService you could filter for these actions like this:

public class MyService extends IntentService {      public MyService() {         super("MyService");     }      @Override     protected void onHandleIntent(Intent intent) {         if(intent.getAction().equals("com.x.y.DOWNLOAD_DATA"){             //download data here         }else if(intent.getAction().equals("com.x.y.UPLOAD_DATA"){             // upload data here         }     } } 

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

like image 94
Andy Res Avatar answered Oct 21 '22 06:10

Andy Res


You can use intent filters for explicitly calling your service or get your service to be implicitly called where components from any application installed on the user's device can potentially start your service

If you plan on using your service only locally (other applications do not use it), then you don't need to (and should not) supply any intent filters

It is clearly specified in the documentation Declaring a service in the manifest

like image 28
anz Avatar answered Oct 21 '22 08:10

anz