Intent Object And Intent Filters In Android Application

In this article, you will learn about intent object and intent filters in Android applications.

Overview

Hope you got a chance to read some useful articles to start programming in Android application. Given below are some of the links:

So far, I have explained here about the use of intent object to call other activities. Let's just recap and gain some more detailed understanding on how the Intent object performs its magic.

Introduction

In the article Intent in Android, we learned that we can call another activity by passing its action to the constructor of an Intent object

  1. startActivity(new Intent(“com.example.administrator.Fragment2Activity”));   

This action is known as component and it is used to identify the target activity/application that you want to invoke. It can be rewritten by specifying the class name of the activity if it resides in the project, like

  1. Intent intent=new Intent(this, MySecondActivity.class);  
  2.   
  3. startActivity(intent);  

You can also create an Intent object by passing in an action constant and data, as follows

  1. Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.in"));  
  2. startActivity(intent);   

The action portion defines what you want to do, while the data portion contains the data for the target activity to act upon. It can further extend and pass the data to the Intent object using the setData method.

  1. Intent intent=new Intent(Intent.ACTION_VIEW”); intent.setData(Uri.parse("http://www.google.co.in"));   

In the above example, it is indicated that you want to view a web page with the specified URL. The Android OS will look for all activities that are able to satisfy your request. This process is known as intent resolution.

Coding

So, we have already seen how an activity can invoke another activity using the Intent object. Here, I will discuss about the Intent filters. Here, I added some code in the existing app which I have covered in previous article. Added a blank activity called BrowserActivity and code in AndroidManifest.xml file

  1. <activity  
  2.      android:name=".BrowserActivity"  
  3.      android:label="@string/title_activity_browser" >  
  4.      <intent-filter>  
  5.          <action android:name="android.intent.action.VIEW"/>  
  6.              <action android:name="com.example.administrator.Browser" />  
  7.                  <category android:name="android.intent.category.DEFAULT"/>  
  8.                      <data android:scheme="http"/>  
  9.      </intent-filter>  
  10.  </activity>  
Button is added in activity_main.xml file.
  1. <Button  
  2.      android:id="@+id/buttonlaunchbrowser"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="wrap_content"  
  5.      android:text="Launch C# Corner"  
  6.      android:onClick="onClickLaunchBrowser"  
  7.      />  

onClick() method is defined in the MainActivity.java file.

  1. public void onClickLaunchBrowser(View view){  
  2.      Intent intent = new Intent("com.example.administrator.Browser");  
  3.      intent.setData(Uri.parse("http://www.c-sharpcorner.com"));  
  4.      startActivity(intent);  
  5.  }  

Add a WebView control in the activity_browser.xml file.

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.      xmlns:tools="http://schemas.android.com/tools"  
  3.      android:orientation="vertical"  
  4.      android:layout_width="fill_parent"  
  5.      android:layout_height="fill_parent"  
  6.      tools:context="com.example.administrator.myfragmentapp.BrowserActivity">  
  7.      <WebView  
  8.          android:id="@+id/webview1"  
  9.          android:layout_width="wrap_content"  
  10.          android:layout_height="wrap_content"  
  11.          />  
  12.  </LinearLayout>  

Add code for WebView control in the BrowserActivity.java class.

  1. package com.example.administrator.myfragmentapp;  
  2.    
  3.  import android.app.Activity;  
  4.  import android.net.Uri;  
  5.  import android.os.Bundle;  
  6.  import android.view.Menu;  
  7.  import android.view.MenuItem;  
  8.  import android.webkit.WebView;  
  9.  import android.webkit.WebViewClient;  
  10.    
  11.    
  12.  public class BrowserActivity extends Activity {  
  13.    
  14.    
  15.      @Override  
  16.      protected void onCreate(Bundle savedInstanceState) {  
  17.          super.onCreate(savedInstanceState);  
  18.          setContentView(R.layout.activity_browser);  
  19.    
  20.          Uri uri=getIntent().getData();  
  21.          WebView webview=(WebView)findViewById(R.id.webview1);  
  22.          webview.setWebViewClient(new Callback());  
  23.          webview.loadUrl(uri.toString());  
  24.      }  
  25.      private class Callback extends WebViewClient {  
  26.          @Override  
  27.          public boolean shouldOverrideUrlLoading  
  28.    
  29.                  (WebView webview, String url){  
  30.              return(false);  
  31.          }  
  32.      }  
  33.    
  34.      @Override  
  35.      public boolean onCreateOptionsMenu(Menu menu) {  
  36.          // Inflate the menu; this adds items to the action bar if it is present.  
  37.          getMenuInflater().inflate(R.menu.menu_browser, menu);  
  38.          return true;  
  39.      }  
  40.    
  41.      @Override  
  42.      public boolean onOptionsItemSelected(MenuItem item) {  
  43.          // Handle action bar item clicks here. The action bar will  
  44.          // automatically handle clicks on the Home/Up button, so long  
  45.          // as you specify a parent activity in AndroidManifest.xml.  
  46.          int id = item.getItemId();  
  47.    
  48.          //noinspection SimplifiableIfStatement  
  49.          if (id == R.id.action_settings) {  
  50.              return true;  
  51.          }  
  52.    
  53.          return super.onOptionsItemSelected(item);  
  54.      }  
  55.  }  

Explanation

In the above code, a new named BrowserActivity is created. So first need to declare it in the AndroidManifest.xml file.

  1. <activity  
  2.      android:name=".BrowserActivity"  
  3.      android:label="@string/title_activity_browser" >  
  4.      <intent-filter>  
  5.          <action android:name="android.intent.action.VIEW"/>  
  6.              <action android:name="com.example.administrator.Browser" />  
  7.                  <category android:name="android.intent.category.DEFAULT"/>  
  8.                      <data android:scheme="http"/>  
  9.      </intent-filter>  
  10.  </activity>  

In the <intent-filter> element, there are two actions, category and data. This means that all the other activities can invoke this activity using either the “android.intent.action.VIEW” or the “com.example.administrator.Browser” action. For all activities that you want others to call by using the startActivity(), they need to have the “android.intent.category.DEFAULT” category. If it is not defined, your activity will not be callable by others. The <data> element specifies the type of data expected by the activity. In this case, it expects the data to start with the http:// prefix.

The above <intent-filter> can be rewritten as

  1. <activity  
  2.      android:name=".BrowserActivity"  
  3.      android:label="@string/title_activity_browser" >  
  4.      <intent-filter>  
  5.          <action android:name="android.intent.action.VIEW"/>  
  6.                 <category android:name="android.intent.category.DEFAULT"/>  
  7.                      <data android:scheme="http"/>  
  8.      </intent-filter>  
  9.     <intent-filter>  
  10.              <action android:name="com.example.administrator.Browser" />  
  11.                  <category android:name="android.intent.category.DEFAULT"/>  
  12.                      <data android:scheme="http"/>  
  13.      </intent-filter>  
  14.  </activity>  

Above <intent-filter> is more readable and it logically groups the action, category, and data within an Intent filter.

If you use the ACTION_VIEW action with the data, Android will ask for a selection.

  1. Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));   

If multiple activities match your Intent object, then a dialog called “Complete action using” appears. So in this case, it can be customized by using the createChooser() method from the Intent class.

  1. Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));  
  2.   
  3. startActivity(Intent.createChooser(intent,”Open Browser Using!!”));  

The preceding will change the dialog title to “Open Browser Using!!” instead of action to default open the browser.

We can group our activities into categories by using the <category> element in the intent filter.

  1. <activity  
  2.      android:name=".BrowserActivity"  
  3.      android:label="@string/title_activity_browser" >  
  4.      <intent-filter>  
  5.          <action android:name="android.intent.action.VIEW"/>  
  6.              <action android:name="com.example.administrator.Browser" />  
  7.                  <category android:name="android.intent.category.DEFAULT"/>  
  8.                 <category android:name="android.intent.category.LearnApps"/>  
  9.                      <data android:scheme="http"/>  
  10.      </intent-filter>  
  11.  </activity>  

In this case, the below code will directly invoke the Browser activity.

  1. Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));  
  2.   
  3. Intent.addCategory(“android.intent.category.LearnApps”);  
  4.   
  5. startActivity(Intent.createChooser(intent,”Open Browser Using!!”));  

You added the category to the intent object using the addCategory() method. If you just omit the addCategory() statement, the preceding code will invoke the Browser activity because it will still match the default category which is android.intent.category.DEFAULT. But, if you specify a category that doesn’t match the category defined in the intent filter, it will not work and no activity will be displayed.

  1. Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));  
  2.   
  3. Intent.addCategory(“android.intent.category.ContentProviderApps”);  
  4.   
  5. startActivity(Intent.createChooser(intent,”Open Browser Using!!”));  

In the preceding, code (android.intent.category.ContentProviderApps) doesn’t match any category in the intent filter. Thus, a run-time exception will be raised.

So, you have to add the following category in the intent filter of Browser activity, then the preceding code will work

  1. <activity  
  2.      android:name=".BrowserActivity"  
  3.      android:label="@string/title_activity_browser" >  
  4.      <intent-filter>  
  5.          <action android:name="android.intent.action.VIEW"/>  
  6.              <action android:name="com.example.administrator.Browser" />  
  7.                  <category android:name="android.intent.category.DEFAULT"/>  
  8.                 <category android:name="android.intent.category.LearnApps"/>  
  9.                 <category android:name="android.intent.category.ContentProviderApps"/>  
  10.                      <data android:scheme="http"/>  
  11.      </intent-filter>  
  12.  </activity>  

Thus, you can add multiple categories to an Intent object.

  1. Intent intent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.c-sharpcorner.com"));  
  2.   
  3. Intent.addCategory(“android.intent.category.LearnApps”);  
  4.   
  5. Intent.addCategory(“android.intent.category.ContentProviderApps”);  
  6.   
  7. startActivity(Intent.createChooser(intent,”Open Browser Using!!”));  

It is evident that when using an Intent object with categories, all categories added to the Intent object must fully match with those defined in the intent filter before an activity can be invoked.

Result

Run the application by pressing button Shift + F10 and get the below screen shot.



Click on the button <Lauch C# Corner>, it displays as below screen shot.



Conclusion

In the above article, I discussed about intent object and intent filters along with categories which are defined in the AndroidMenifest.xml file. I will explain about displaying notifications in the next article. If you have any question or comment, post me a message in C# Corner comments section.