Android開發教學
Android 開發環境配置
Android 架構
Android 應用組件
Android Hello World示例
Android 資源組織和訪問
Android Activity
Android Service
Android廣播接收器
Android內容提供者
Android碎片/片段
Android Intent過濾器
Android UI佈局
Android LinearLayout
Android RelativeLayout
Android TableLayout
Android AbsoluteLayout
Android FrameLayout
Android ListView
Android GridView
Android UI控件
Android TextView
Android EditText
Android AutoCompleteTextView
Android Button
Android ImageButton
Android CheckBox
Android ToggleButton
Android RadioButton
Android RadioGroup
Android事件處理
Android樣式和主題
Android樣式示例
Android主題示例
Android自定義組件
Android拖放
Android通知
Android基於位置服務
Android發送電子郵件
Android發送短信/SMS
Android撥打電話
發佈Android應用
ArrayAdapter
SimpleCursorAdapter
Android ProgressDialog
Android Spinner
使用活動代碼自定義Android組件
使用佈局文件自定義Android組件
Android自定義組件及屬性
Android Alertdialog(警告對話框)
Android Animation(動畫)實例
Android音頻捕獲(錄音)
Android音頻管理器實例
Android AutoCompleteTextView(自動完成)實例
Android最佳實踐
Android Bluetooth(藍牙)實例
Android Camera(攝像頭)
Android Clipboard(複製/剪貼板)
Android自定義字體
Android數據備份
Android Gestures/手勢
Android圖片效果
Android圖片切換
Android內部存儲
Android JetPlayer實例
Android JSON解析器
Android加載Spinner
Android本地化
Android登錄實例
Android MediaPlayer(多媒體播放)

Android撥打電話

每一個Android設備特別是手機都提供一個撥打電話功能,但仍然需要編寫一個應用程序,給用戶一個選擇使用硬編碼的電話號碼撥打電話。

本章列出了一個簡單的步驟來創建一個應用程序,它可以用來撥打電話。使用 Android 的 Intent 通過調用Android內置的電話通話功能。以下部分說明 Intent 對象的撥打電話功能 。

Intent 對象 - 操作撥打電話

使用 ACTION_CALL 動作觸發Android設備內置電話功能。以下是簡單的語法用來創建一個Intent 的 ACTION_CALL 動作

Intent phoneIntent = new Intent(Intent.ACTION_CALL);

可以使用 ACTION_DIAL 動作,而不是 ACTION_CALL,在這種情況下,在使用選項來修改硬編碼的電話號碼撥打電話之前,而不是直接調用的。

Intent 對象 - 數據/電話呼叫類型

這裏給定電話爲 13800138000 撥打一個電話,需要使用setData()方法指定URI爲  tel:如下:

phoneIntent.setData(Uri.parse("tel:13800138000"));

要注意的一點是,撥打電話不需要任何額外的數據或數據類型指定。

示例

下面的示例演示如何在實際使用 Android Intent 打電話給定的手機號碼。

要嘗試這個例子中,需要實際配備了最新的 Android OS 移動設備,否則仿真器可能無法正常工作。

步驟

描述

1

使用Android Studio創建Android應用程序,並將它命名爲PhoneCallDemounder。創建這個項目,確保目標 SDK編譯在 Android SDK 的最新版本或使用更高級別的API

2

修改 src/MainActivity.java 文件,並添加所需的代碼,以撥打電話

3

修改所需的佈局XML文件 res/layout/activity_main.xml 添加GUI組件。添加一個簡單的按鈕來撥打號碼:13800138000

4

修改  res/values/strings.xml  定義所需的常數值

5

修改 AndroidManifest.xml 如下所示

6

運行該應用程序啓動 Android模擬器並驗證應用程序所做的修改結果

以下是修改主活動文件 src/com.yiibai.phonecalldemo/MainActivity.java 的內容如下:

package com.yiibai.phonecalldemo; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startBtn = (Button) findViewById(R.id.makeCall); startBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { makeCall(); } }); } protected void makeCall() { Log.i("Make call", ""); Intent phoneIntent = new Intent(Intent.ACTION_CALL); phoneIntent.setData(Uri.parse("tel:91-800-001-0101")); try { startActivity(phoneIntent); finish(); Log.i("Finished making a call...", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "Call faild, please try again later.", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

下面是 res/layout/activity_main.xml 文件的內容:

<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/makeCall" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/make_call"/>

下面文件 res/values/strings.xml 的內容中定義兩個新的常量:

<string name="app_name">PhoneCallDemo <string name="hello_world">Hello world! <string name="action_settings">Settings <string name="make_call">Call 91-800-001-0101

以下是AndroidManifest.xml 文件的默認內容:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yiibai.phonecalldemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yiibai.phonecalldemo.MainActivity" android:label="@string/app_name" > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />

我們嘗試運行PhoneCallDemo 應用程序。Eclipse AVD安裝的應用程序,並啓動它,如果一切設置和應用代碼都沒有問題,它會顯示以下模擬器窗口: 

Android

選擇移動設備作爲一個選項,然後檢查移動設備,這將顯示以下畫面:

Android撥打電話

現在使用按鈕撥打138001380000,如下所示:

Android撥打電話

以上代碼下載:  http://pan.baidu.com/s/1hq1RSuK