In addition to the user defined activities, an android application contains a number of built-in activities such as browser, contacts, call_ dallier etc. In this part we are going to see how to call a built-in activity from your own activity. Like the calling of user defined activities a built in activity is also called using the intent object.
We will start with an example, for that create a new android project and named it as BuiltinApp. In this project the main.xml file contain two buttons, first one is for invoking the built-in activity browser and the other one is for dialing a particular number.
The main.xml file for the project is given below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/browser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Browser" />
<Button
android:id="@+id/start_call"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Make a Call" />
</LinearLayout>
The BuiltinAppActivity.java file is shown bellow.
package com.codeglympse;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BultinAppActivity extends Activity {
Button b1;
Button b2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) findViewById(R.id.browser);
b2 = (Button) findViewById(R.id.start_call);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(i);
}
});
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:+571894567"));
startActivity(i);
}
});
}
}
In the above example we call two built in activities, the browser and dial. Here we also passing a data to the called activity by using the Uri.parse() method. That means here the intent object is an used as an action and data pair object.
Intent i = new Intent(android.content.Intent.ACTION_DIAL,Uri.parse("tel:+571894567"));
Intent i =new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
For invoking the built in activities like browser, you have set the android permission in the androidmanifest.xml file.The permission is set within the <manifest> </manifest> tag in AndroidManifest.xml file.
AndroidManifest,xml file is given bellow.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codeglympse"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BultinAppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" > </uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"> </uses-permission>
</manifest>
Output of the project is given bellow.