Please return following links to the footer:
Designed by Blog templates, thanks to: Free creative WordPress themes, Ultrasound Diagnoses and sizzling hot
What is Intent Filter in Android?

What is Intent Filter in Android?

Intent Filter in Android

Intent Filters in Android is a very powerful way to connect different applications together which allows greater reuse and makes a user experience transparent to the fact that more than one application is used to achieve a certain task.Intent Filter in Android is a Activities and intent receivers include one or more filters in their manifest to describe what kinds of intents or messages they can handle or want to receive. Intent Filter in android lists a set of  requirements, such as data type, action requested and URI formate, that the intent or message must fulfill. For Activities, Android searches for the Activity with the most closely matching valid match between the intent and the  intent filter in Android .For messages, Android Will forward a messages to all receivers with the matching  Intent Filter in Android .

in my previous post,What is intent in android? I have disused about intents. In that post we have seen that there are two types of intents.

1. Explicit intents: We can call target component by its name.
2. implicit intents: We can call a target component on the basis of what we want to perform.

In this post we will see, how does implicit intent work?
According to google intent filter is “To inform the system which implicit intents they can handle”

e.g. you have more that one activities in your application and you know that there is no “main” method in android application. So how activity launcher will know that which activity to start first.

For that we specify a intent filter in android.

  1. <activity android:name=”.HomeActivity” android:label=”@string/app_name”>
  2.             <intent-filter>
  3.                 <action android:name=”android.intent.action.MAIN”>
  4.                 <category android:name=”android.intent.category.LAUNCHER”>
  5.             </category></action></intent-filter>
  6. </activity>

intent filter in android with “android.intent.action.MAIN” action and “android.intent.category.LAUNCHER” category tells to launcher to launch HomeActivity.

implicit Intent is luxury provided by android. You just need to know, what do you want to perform. you don’t bother who is going to perform and how that activity is going to perform.(there will be a problem, if no activity is there to perform your task)

When will you specify intent filter in android?

if you think, any other application can use your activity to perform some task, specify a intent filter for that. You can be a performer using intent filter in android.

For example, you have created a activity that can display image and you think that other applications also use your activity to view images.

1. specify intent filter like this.

  1. <activity android:label=”@string/app_name” android:name=”.MyImageViewerActivity”>
  2.             <intent-filter>
  3.                 <action android:name=”android.intent.action.MAIN”>
  4.                 <category android:name=”android.intent.category.LAUNCHER”>
  5.             </category></action></intent-filter>
  6.             <intent-filter>
  7.                 <action android:name=”android.intent.action.VIEW”>
  8.                 <category android:name=”android.intent.category.DEFAULT”>
  9.                 <data android:mimetype=”image/*”>
  10.             </data></category></action></intent-filter>
  11. </activity>

In the above code we can see that intent-filter has following properties.
a. action: type for which this activity will respond for.Like this activity will respond for view action.
b. category: implicit intents are divided in categories.Like this activity is in default category.
c. data: Specify more information about intent-filter. Like this activity will handle file of mimeType “image/*”.

2. Now you need to display image in your activity.

  1. public class MyImageViewerActivity extends Activity {
  2.     TextView textView;
  3.     ImageView imageView;
  4.     /** Called when the activity is first created. */
  5.     @Override
  6.     public void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.         Intent intent = getIntent();
  10.         Uri uri = intent.getData();
  11.         textView = (TextView) findViewById(R.id.textView);
  12.         imageView = (ImageView) findViewById(R.id.imageView);
  13.         if(uri != null){
  14.             imageView.setImageURI(uri);
  15.             textView.setText(uri.getPath());
  16.         }
  17.     }
  18. }

using getData() we will get the path of the image and we will use that path to display image.

3. When you install this application in your device and click on any image file, you will see a choice dialog that offer you to select between default application and your application. Just select your application to see the image.

intent-filter in android

 

4. Just select your app to view image:

intent-filter in android-activity1

When will you call implicit intent?

If you think that any other activity is available in device for handling your application’s specific task, just use the implicit intent to call that activity.

For example, if you want to open a browser to render URI. You can use implicit intent to call browser.

  1. Uri uri = Uri.parse( ”http://blog.vimviv.com” );
  2. startActivity(new Intent(Intent.ACTION_VIEW, uri));

In above example we can see that we have a uri and we want to view content of that uri. We don’t need to know that which activity is going to render this uri.

Some more examples of intent filters in android.

a) intent filter for app launcher

  1. <intent-filter>
  2.     <action android:name=”android.intent.action.MAIN”>
  3.     <category android:name=”android.intent.category.LAUNCHER”>
  4. </category></action></intent-filter>

b)intent filter for image view.

  1. <intent-filter>
  2.     <action android:name=”android.intent.action.VIEW”>
  3.     <category android:name=”android.intent.category.DEFAULT”>
  4.     <data android:mimetype=”image/*”>
  5. </data></category></action></intent-filter>

c)intent filter for image send:

  1. <intent-filter>
  2.     <action android:name=”android.intent.action.SEND”>
  3.     <category android:name=”android.intent.category.DEFAULT”>
  4.     <data android:mimetype=”image/*”>
  5. </data></category></action></intent-filter>

d) intent filter for custom uri:

  1. <intent-filter>
  2.     <action android:name=”android.intent.action.VIEW”>
  3.     <category android:name=”android.intent.category.DEFAULT”>
  4.     <category android:name=”android.intent.category.BROWSABLE”>
  5.     <data android:scheme=”myapp” android:host=”path”>
  6. </data></category></category></action></intent-filter>

e) intent filter for sms:

  1. <intent-filter>
  2.     <action android:name=”android.intent.action.VIEW”>
  3.     <action android:name=”android.intent.action.SENDTO”>
  4.     <category android:name=”android.intent.category.DEFAULT”>
  5.     <category android:name=”android.intent.category.BROWSABLE”>
  6.     <data android:scheme=”sms”>
  7.     <data android:scheme=”smsto”>
  8. </data></data></category></category></action></action></intent-filter>

f)intent filter phone dial:

  1. <intent-filter>
  2.     <action android:name=”android.intent.action.VIEW”>
  3.     <action android:name=”android.intent.action.DIAL”>
  4.     <category android:name=”android.intent.category.DEFAULT”>
  5.     <category android:name=”android.intent.category.BROWSABLE”>
  6.     <data android:scheme=”tel”>
  7. </data></category></category></action></action></intent-filter>

Some examples for calling implicit intents:

a) start contacts activity:

  1. Intent contacts = new Intent();  
  2.               contacts.setAction(android.content.Intent.ACTION_VIEW);
  3.               contacts.setData(People.CONTENT_URI);
  4.               startActivity(contacts);

b) install an app:

  1. Intent intent = new Intent(Intent.ACTION_VIEW);
  2. intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + ”app.apk”)), ”application/vnd.android.package-archive”);
  3. startActivity(intent);

c) uninstall an app:

  1. Uri packageUri = Uri.parse(“package:com.vimviv”);
  2.             Intent uninstallIntent =
  3.               new Intent(Intent. ACTION_DELETE, packageUri);
  4.             startActivity(uninstallIntent);

d) start sms application:

  1. Intent sendIntent = new Intent(Intent.ACTION_VIEW);
  2. sendIntent.setData(Uri.parse(“sms:”));

We can also search for a matching intent:

  1. final PackageManager packageManager = context.getPackageManager();
  2.     final Intent intent = new Intent(action);
  3.     List<resolveinfo> list =
  4.             packageManager.queryIntentActivities(intent,
  5.                     PackageManager.MATCH_DEFAULT_ONLY);
  6.     return list.size() > 0;
  7. </resolveinfo>

 

Share and Enjoy

  • Facebook
  • Twitter
  • LinkedIn
  • Email
Email
Print