Intent Object And Intent Filters In Android Application

Overview

 
I 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 an 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 bypassing 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. 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 the Intent filters. Here, I added some code in the existing app which I have covered in the 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>    
The button is added to the 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.  public class BrowserActivity extends Activity {    
  11.      @Override    
  12.      protected void onCreate(Bundle savedInstanceState) {    
  13.          super.onCreate(savedInstanceState);    
  14.          setContentView(R.layout.activity_browser);    
  15.      
  16.          Uri uri=getIntent().getData();    
  17.          WebView webview=(WebView)findViewById(R.id.webview1);    
  18.          webview.setWebViewClient(new Callback());    
  19.          webview.loadUrl(uri.toString());    
  20.      }    
  21.      private class Callback extends WebViewClient {    
  22.          @Override    
  23.          public boolean shouldOverrideUrlLoading    
  24.      
  25.                  (WebView webview, String url){    
  26.              return(false);    
  27.          }    
  28.      }    
  29.      
  30.      @Override    
  31.      public boolean onCreateOptionsMenu(Menu menu) {    
  32.          // Inflate the menu; this adds items to the action bar if it is present.    
  33.          getMenuInflater().inflate(R.menu.menu_browser, menu);    
  34.          return true;    
  35.      }    
  36.      
  37.      @Override    
  38.      public boolean onOptionsItemSelected(MenuItem item) {    
  39.          // Handle action bar item clicks here. The action bar will    
  40.          // automatically handle clicks on the Home/Up button, so long    
  41.          // as you specify a parent activity in AndroidManifest.xml.    
  42.          int id = item.getItemId();    
  43.      
  44.          //noinspection SimplifiableIfStatement    
  45.          if (id == R.id.action_settings) {    
  46.              return true;    
  47.          }    
  48.      
  49.          return super.onOptionsItemSelected(item);    
  50.      }    
  51.  }   
 
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 screenshot.
 
 
Click on the button <Lauch C# Corner>, it displays as below screenshot.
 
 

Conclusion

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


Similar Articles