Android 程式開發:(二)使用意圖 —— 2.5 使用Intent調用內建應用程式程式

來源:互聯網
上載者:User

我們已經瞭解了如何在自己的單個應用中調用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按鈕:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.