Use Intent Filters

Description

The following code shows how to Use Intent Filters.

Example

Set up manifest file


      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.java2s.Intents"
          android:versionCode="1"
          android:versionName="1.0" >

          <uses-sdk android:minSdkVersion="14" />
          <uses-permission android:name="android.permission.CALL_PHONE"/>
          <uses-permission android:name="android.permission.INTERNET"/>
          <application
              android:icon="@drawable/ic_launcher"
              android:label="@string/app_name" >
              <activity
                  android:label="@string/app_name"
                  android:name=".IntentsActivity" >
                  <intent-filter >
                      <action android:name="android.intent.action.MAIN" />
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
              <activity android:name=".MyBrowserActivity"
                        android:label="@string/app_name">
                  <intent-filter>
                      <action android:name="android.intent.action.VIEW" />
                      <action android:name="com.java2s.MyBrowser" />
                      <category android:name="android.intent.category.DEFAULT" />
                      <data android:scheme="http" />
                  </intent-filter>
              </activity>

          </application>

      </manifest>

Add the following statements to the MainActivity.java file:


//www  . j  a v a2s  .c o  m
package com.java2s.myapplication3.app;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
    int request_Code = 1;

    public void onClickLaunchMyBrowser(View view) {
        Intent i = new Intent("com.java2s.MyBrowser");
        i.setData(Uri.parse ("http://www.java2s.com"));
        startActivity(i);
    }

}

Add the following statements to the activity_main.xml file:



      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      <WebView
          android:id="@+id/WebView01"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
      </LinearLayout>

Add the following statements to the MyBrowserActivity.java file:


      import android.app.Activity;
      import android.net.Uri;
      import android.os.Bundle;
      import android.webkit.WebView;
      import android.webkit.WebViewClient;
/*from ww  w . ja va2  s  .  c  o m*/
      public class MyBrowserActivity extends Activity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.browser);

              Uri url = getIntent().getData();
              WebView webView = (WebView) findViewById(R.id.WebView01);
              webView.setWebViewClient(new Callback());
              webView.loadUrl(url.toString());
             }

             private class Callback extends WebViewClient {
                 @Override
                 public boolean shouldOverrideUrlLoading
                 (WebView view, String url) {
                     return(false);
                 }
             }
         }

Note

In the AndroidManifest.xml file you have the following definition.


...
                 <activity android:name=".MyBrowserActivity"
                           android:label="@string/app_name">
                     <intent-filter>
                         <action android:name="android.intent.action.VIEW" />
                         <action android:name="com.java2s.MyBrowser" />
                         <category android:name="android.intent.category.DEFAULT" />
                         <data android:scheme="http" />
                     </intent-filter>
                 </activity>
...

In the <intent-filter> element, you declared it to have two actions, one category, and one data.

This means that all other activities can invoke this activity using either the "android.intent.action.VIEW" or the "com.java2s.MyBrowser" action.

Activities need to have the "android.intent.category.DEFAULT" category in order to call the startActivity() or startActivityForResult() methods.

The <data> element specifies the type of data expected by the activity. The code above expects the data to start with the "http://" prefix.

Note 2

The preceding intent filter could also be rewritten as follows:


                 <activity android:name=".MyBrowserActivity"
                           android:label="@string/app_name">
                     <intent-filter>
                         <action android:name="android.intent.action.VIEW" />
                         <category android:name="android.intent.category.DEFAULT" />
                         <data android:scheme="http" />
                     </intent-filter>
                     <intent-filter>
                         <action android:name="com.java2s.MyBrowser" />
                         <category android:name="android.intent.category.DEFAULT" />
                       <data android:scheme="http" />
                   </intent-filter>
               </activity>

The intent filter in this way is much more readable, and it logically groups the action, category, and data within an intent filter.

Note 3

Suppose we create an Intent as follows:


Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                      Uri.parse ("http://www.java2s.com"));

Android will display a selection.

You can choose between using the Browser application or the Intents application that you are currently building.

When multiple activities match your Intent object, the dialog titled "Complete action using" appears.

You can customize this by using the createChooser() method from the Intent class, like this:


Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse ("http://www.amazon.com"));
startActivity(Intent.createChooser(i, "Open URL using..."));




















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS