【Android 开发教程】使用Intent调用内置应用程序

来源:互联网 发布:台湾手机网络制式 编辑:程序博客网 时间:2024/05/22 13:14

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


我们已经了解了如何在自己的单个应用中调用activity。但是,android开发中比较重要的一点,就是使用intent调用其他应用的activity。特别地,你的应用可以调用系统中的许多“内置”应用。所谓的“内置”应用,指的就是系同级别的应用,比如Browser,Phone,Sms等等。举个例子,如果你的应用需要打开一个网页,可以使用Intent对象去调用浏览器,浏览器把网页显示出来,而不是要自己创建一个浏览器。。。

下面的例子展示如何调用系统中的几个比较常用的“内置”应用。

1. 创建一个工程,Intents。

2. main.xml中的代码。

[java] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn_webbrowser"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:onClick="onClickWebBrowser"  
  12.         android:text="Web Browser" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/btn_makecalls"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:onClick="onClickMakeCalls"  
  19.         android:text="Make Calls" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/btn_showMap"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:onClick="onClickShowMap"  
  26.         android:text="Show Map" />  
  27.   
  28.     <Button  
  29.         android:id="@+id/btn_launchMyBrowser"  
  30.         android:layout_width="fill_parent"  
  31.         android:layout_height="wrap_content"  
  32.         android:onClick="onClickLaunchMyBrowser"  
  33.         android:text="Launch My Browser" />  
  34.   
  35. </LinearLayout>  
3. IntentsActivity.java中的代码。
[java] view plaincopy
  1. public class IntentsActivity extends Activity {  
  2.   
  3.     int request_Code = 1;  
  4.   
  5.     /** Called when the activity is first created. */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.     }  
  11.   
  12.     public void onClickWebBrowser(View view) {  
  13.         Intent i = new Intent("android.intent.action.VIEW");  
  14.         i.setData(Uri.parse("http://www.amazon.com"));  
  15.         startActivity(i);  
  16.     }  
  17.   
  18.     public void onClickMakeCalls(View view) {  
  19.         Intent i = new Intent(android.content.Intent.ACTION_DIAL,  
  20.                 Uri.parse("tel:+651234567"));  
  21.         startActivity(i);  
  22.   
  23.     }  
  24.   
  25.     public void onClickShowMap(View view) {  
  26.         Intent i = new Intent(android.content.Intent.ACTION_VIEW,  
  27.                 Uri.parse("geo:37.827500,-122.481670"));  
  28.         startActivity(i);  
  29.   
  30.     }  
  31.   
  32. }  
4. 调试。


程序启动之后:

点击WebBrowser按钮:

点击MakeCalls按钮:

原创粉丝点击