Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

第72章、再識 Intent — 呼叫撥號程式(從零開始學 Android)

編輯:Android 技術基礎

在 Android 中要進行電話撥號,可以透過設定 ACTION_CALL 或 ACTION_DIAL 來達成。兩者的差異在於:

(1)ACTION_CALL:直接撥出號碼;
(2)ACTION_DIAL:開啟撥號程式,由使用者手動按下通話鍵。

一、設計介面

1. MainActivity 版面配置

開啟 res/layout/activity_main.xml,加入號碼輸入框與呼叫按鈕:

[html] view plain copy  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="https://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/tel"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="請輸入電話號碼:" />  
  13.   
  14.     <EditText  
  15.         android:id="@+id/telphone"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:ems="10" >  
  19.   
  20.         <requestFocus />  
  21.     </EditText>  
  22.   
  23.     <Button  
  24.         android:id="@+id/call"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="呼叫" />  
  28.   
  29. </LinearLayout>  

二、程式碼實作

開啟 src/com.genwoxue.intenttel/MainActivity.java,加入以下程式碼:

[java] view plain copy  
  1. package com.genwoxue.intenttel;  
  2.   
  3.   
  4. import android.net.Uri;  
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.content.Intent;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.TextView;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     private TextView tvTelphone=null;  
  16.     private Button btnCall=null;  
  17.     @Override  
  18.      public void onCreate(Bundle savedInstanceState)     
  19.     {     
  20.         super.onCreate(savedInstanceState);                
  21.         setContentView(R.layout.activity_main);  
  22.         tvTelphone=(TextView)super.findViewById(R.id.telphone);  
  23.         btnCall=(Button)super.findViewById(R.id.call);  
  24.         btnCall.setOnClickListener(new OnClickListener(){  
  25.             public void onClick(View v)  
  26.             {    
  27.                 String Telphone=tvTelphone.getText().toString();  
  28.                 Uri uri=Uri.parse("tel:"+Telphone);  
  29.                 Intent intent=new Intent();  
  30.                 intent.setAction(Intent.ACTION_CALL);  
  31.                 intent.setData(uri);  
  32.                 MainActivity.this.startActivity(intent);  
  33.             }  
  34.         });  
  35.     }  
  36.         
  37. }             

三、設定檔

開啟 AndroidManifest.xml,加入撥號權限:

[html] view plain copy  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="https://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.intenttel"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="15" />  
  10.       
  11.     <span style="color:#ff0000;"><strong><uses-permission android:name="android.permission.CALL_PHONE"/></strong>  
  12. </span>  
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.genwoxue.intenttel.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.                 </intent-filter>  
  25.             </activity>  
  26.     </application>  
  27.   
  28. </manifest>  

四、執行結果

 
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved