We have learned how to call an activity in a single application. However, an important aspect in android development is to use intent to call the activity of other applications. In particular, your application can call many "built-in" applications in the system. The so-called "built-in" application refers to applications at the same level, such as Browser, Phone, and Sms. For example, if your application needs to open a webpage, you can use the Intent object to call the browser. The browser displays the webpage instead of creating a browser...
The following example shows how to call several commonly used "built-in" applications in the system.
1. Create a project called Intents.
Project directory:
2. Code in main. xml.
<? 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">
<Button
Android: id = "@ + id/btn_webbrowser"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClickWebBrowser"
Android: text = "Web Browser"/>
<Button
Android: id = "@ + id/btn_makecils"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onclickmakecils"
Android: text = "Make CILS"/>
<Button
Android: id = "@ + id/btn_showMap"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClickShowMap"
Android: text = "Show Map"/>
<Button
Android: id = "@ + id/btn_launchMyBrowser"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: onClick = "onClickLaunchMyBrowser"
Android: text = "Launch My Browser"/>
</LinearLayout>
3. The code in IntentsActivity. java. Package net. learn2develop. Intents;
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Public class IntentsActivity extends Activity {
Int request_Code = 1;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public void onClickWebBrowser (View view ){
Intent I = new Intent ("android. intent. action. VIEW ");
I. setData (Uri. parse ("http://www.amazon.com "));
StartActivity (I );
}
Public void onclickmakecils (View view ){
Intent I = new Intent (android. content. Intent. ACTION_DIAL,
Uri. parse ("tel: + 651234567 "));
StartActivity (I );
}
Public void onClickShowMap (View view ){
Intent I = new Intent (android. content. Intent. ACTION_VIEW,
Uri. parse ("geo: 37.827500,-122.481670 "));
StartActivity (I );
}
}
4. debugging.
:
After the program is started:
Click WebBrowser:
Click the makecils button:
Written by manoel