Dealing With Intent Object And Intent Filters
×
Home Courses Community Support
Gray Matter

First Step Towards Android: Introduction »

Understanding Activities and Intents »

Hands On With UI Designing In Android (Controls-Views,Widgets and Layouts) »

Updating... »

Dealing With Intent Object And Intent Filters

So far, you have seen the use of the Intent object to call other activities. This is a good time to recap and gain a more detailed understanding of how the Intent object performs its magic. First, you see that you can call another activity by passing its action to the constructor of an Intent object: startActivity(new Intent(“net.learn2develop.ACTIVITY2”)); The action (in this example “net.learn2develop.ACTIVITY2”) is also known as the component name. This is used to identify the target activity/application that you want to invoke. You can also rewrite the component name by specifying the class name of the activity if it resides in your project, like this: startActivity(new Intent(this, Activity2.class));

You can also create an Intent object by passing in an action constant and data, such as the following:

Intent i = new
Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(“http://www.amazon.com”));
startActivity(i);


The action portion defines what you want to do, while the data portion contains the data for the target activity to act upon. You can also pass the data to the Intent object using the setData() method:

Intent i = new
Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse(“http://www.amazon.com”));


In this example, you indicate 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. The next section discusses in more detail how your activities can be the target of other activities. For some intents, there is no need to specify the data. For example, to select a contact from the Contacts application, you specify the action and then indicate the MIME type using the setType() method:

Intent i = new
Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);


The setType() method explicitly specifies the MIME data type to indicate the type of data to return. The MIME type for ContactsContract.Contacts.CONTENT_TYPE is “vnd.android.cursor.dir/contact”.

Besides specifying the action, the data, and the type, an Intent object can also specify a category. A category groups activities into logical units so that Android can use it for further filtering. The next section discusses categories in more details.

To summarize, an Intent object can contain the following information:

  • Action
  • Data
  • Type
  • Category

Play with intent filters

Earlier, you saw how an activity can invoke another activity using the Intent object. In order for other activities to invoke your activity, you need to specify the action and category within the <intent-filter> element in the AndroidManifest.xml file, like this:

<intent-filter>
<action android:name=”net.learn2develop.ACTIVITY2” />
<category android:name=”android.intent.category.DEFAULT” />
</intent-filter>


This is a very simple example in which one activity calls another using the “net.learn2develop.ACTIVITY2” action. The following Try It Out shows you a more sophisticated example.

  • Using the Intents project created earlier, add a new class to the project and name it MyBrowserActivity.java. Also add a new XML file to the res/layout folder and name it browser.xml.
  • Add the following statements in bold to the AndroidManifest.xml file:
<?xml version=”1.0” encoding=”utf-8”?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”net.learn2develop.Intents”
android:versionCode=”1”
android:versionName=”1.0”>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.MainActivity”
android:label=”@string/app_name”>
<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=”net.learn2develop.MyBrowser” />
<category android:name=”android.intent.category.DEFAULT” />
<data android:scheme=”http” />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion=”9” />
<uses-permission android:name=”android.permission.CALL_PHONE” />
<uses-permission android:name=”android.permission.INTERNET” />
</manifest>

 

  • Add the following statements in bold to the 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” >
<Button
android:id=”@+id/btn_webbrowser”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Web Browser” />
<Button
android:id=”@+id/btn_makecalls”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Make Calls” />
<Button
android:id=”@+id/btn_showMap”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Show Map” />
<Button
android:id=”@+id/btn_chooseContact”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Choose Contact” />
<Button
android:id=”@+id/btn_launchMyBrowser”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Launch My Browser” />
</LinearLayout>

 

  • Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Intents;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1, b2, b3, b4, b5;
int request_Code = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//---Web browser button---
b1 = (Button) findViewById(R.id.btn_webbrowser);
b1.setOnClickListener(new OnClickListener()
{
//...
});
//---Make calls button---
b2 = (Button) findViewById(R.id.btn_makecalls);
b2.setOnClickListener(new OnClickListener()
{
//...
});
//---Show Map button---
b3 = (Button) findViewById(R.id.btn_showMap);
b3.setOnClickListener(new OnClickListener()
{
//...
});
//---Choose Contact button---
b4 = (Button) findViewById(R.id.btn_chooseContact);
b4.setOnClickListener(new OnClickListener()
{
//...
});
b5 = (Button) findViewById(R.id.btn_launchMyBrowser);
b5.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent i = new
Intent(“net.learn2develop.MyBrowser”);
i.setData(Uri.parse(“http://www.amazon.com”));
startActivity(i);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
//...
}
}

 

  • Add the following statements in bold to the browser.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 in bold to the MyBrowserActivity.java file:
package net.learn2develop.Intents;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyBrowserActivity extends Activity {
            /** Called when the activity is first created. */
           @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);
}
}
}

 

  • Press F11 to debug the application on the Android Emulator.
  • Click the Launch my Browser button and you should see the new activity displaying the Amazon.com web page.

Intent - Amazon

 

 

← Preview

Calling Built in Apps

Next →

Displaying Notifications

NOTES

Our mission is to provide highly effective and quality education via innovative solutions. Geeksdemy look forward to bridge the gap between in-demand technology and academics in order to deliver innovative, easy, interesting and affordable learning across the Globe.

Courses

  • Python
  • Game Development with Unity 3D
  • Arduino
  • PCB & Circuit Designing
  • Robotics and Embedded C
  • 8051/8052 Embedded Systems
  • QuadCopter & UAV
  • MATLAB with Robotics
  • Raspberry Pi
Learn On the Go!
Get the Android App
Get the iOS App