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.
intent filter 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?
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.
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.
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.
public class MyImageViewerActivity extends Activity {
TextView textView;
ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
Uri uri = intent.getData();
textView = (TextView) findViewById(R.id.textView);
imageView = (ImageView) findViewById(R.id.imageView);
if(uri != null){
imageView.setImageURI(uri);
textView.setText(uri.getPath());
}
}
}
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.
4. Just select your app to view image:

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.
Uri uri = Uri.parse( "http://blog.vimviv.com" ); 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.
a) intent filter for app launcher
b)intent filter for image view.
c)intent filter for image send:
d) intent filter for custom uri:
e) intent filter for sms:
f)intent filter phone dial:
Some examples for calling implicit intents:
a) start contacts activity:
Intent contacts = new Intent(); contacts.setAction(android.content.Intent.ACTION_VIEW); contacts.setData(People.CONTENT_URI); startActivity(contacts);
b) install an app:
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "app.apk")), "application/vnd.android.package-archive"); startActivity(intent);
c) uninstall an app:
Uri packageUri = Uri.parse("package:com.vimviv");
Intent uninstallIntent =
new Intent(Intent. ACTION_DELETE, packageUri);
startActivity(uninstallIntent);
d) start sms application:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
We can also search for a matching intent:
final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); Listlist = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0;
So help others by creating re-usable activities.
Incoming search terms:
- what is intent filter in android (90)
- what is the use of intent filter in android (8)
- intent filter in android example (6)
- intent filter example (6)
- what is intent filters in android (4)
- why we use intent filter in android (3)
- what is intent filter (3)
- intent mimeType=image/* getintent (2)
- intent filter example in android (2)
- why we use intent-filter in android (2)
