• Android Video Tutorials

Android - 電話呼叫



Android 提供了內建的電話呼叫應用程式,在某些情況下,我們可能需要透過我們的應用程式撥打電話。這可以透過使用具有適當操作的隱式 Intent 來輕鬆完成。此外,為了監控裝置上某些電話狀態的變化,我們可以使用 PhoneStateListener 和 TelephonyManager 類。

本章列出了建立可用於撥打電話的應用程式的所有簡單步驟。您可以使用 Android Intent 透過呼叫 Android 的內建電話呼叫功能來撥打電話。以下部分解釋了撥打電話所需的 Intent 物件的不同部分。

Intent 物件 - 撥打電話的操作

您將使用 **ACTION_CALL** 操作來觸發 Android 裝置中可用的內建電話呼叫功能。以下是使用 ACTION_CALL 操作建立 Intent 的簡單語法

Intent phoneIntent = new Intent(Intent.ACTION_CALL);

您可以使用 **ACTION_DIAL** 操作代替 ACTION_CALL,在這種情況下,您可以在撥打電話之前修改硬編碼的電話號碼,而不是直接撥打電話。

Intent 物件 - 撥打電話的資料/型別

要撥打給定號碼 91-000-000-0000 的電話,您需要使用 setData() 方法指定 **tel:** 作為 URI,如下所示:

phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));

有趣的是,要撥打電話,您不需要指定任何額外的資料或資料型別。

示例

以下示例向您展示瞭如何在實踐中使用 Android Intent 來撥打給定手機號碼的電話。

要試驗此示例,您需要配備最新 Android 作業系統的實際移動裝置,否則您將不得不使用可能無法工作的模擬器。
步驟 描述
1 您將使用 Android Studio IDE 建立一個 Android 應用程式,並將其命名為 _我的應用程式_,包名為 _com.example.saira_000.myapplication_。
2 修改 _src/MainActivity.java_ 檔案並新增必要的程式碼來處理撥打電話。
3 修改佈局 XML 檔案 _res/layout/activity_main.xml_,如果需要,新增任何 GUI 元件。我添加了一個簡單的按鈕來撥打 91-000-000-0000 號碼
4 無需定義預設字串常量。Android Studio 會處理預設常量。
5 修改 _AndroidManifest.xml_,如下所示
6 執行應用程式以啟動 Android 模擬器並驗證對應用程式所做的更改的結果。

以下是修改後的主活動檔案 **src/MainActivity.java** 的內容。

package com.example.saira_000.myapplication;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
   private Button button;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      button = (Button) findViewById(R.id.buttonCall);
		
      button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View arg0) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:0377778888"));
				
            if (ActivityCompat.checkSelfPermission(MainActivity.this,
               Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                  return;
               }
               startActivity(callIntent);
         }
      });

   }
}

以下是 **res/layout/activity_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/buttonCall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="call 0377778888" />

</LinearLayout>

以下是 **res/values/strings.xml** 檔案的內容,用於定義兩個新的常量:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Application</string>
</resources>

以下是 **AndroidManifest.xml** 的預設內容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.saira_000.myapplication" >
   
   <uses-permission android:name="android.permission.CALL_PHONE" />
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.saira_000.myapplication.MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
      
   </application>
</manifest>

讓我們嘗試執行您的 **我的應用程式** 應用程式。我假設您已將實際的 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟專案中的一個活動檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示以下螢幕:

Android Mobile Call Screen

現在使用 **呼叫** 按鈕撥打電話,如下所示:

Android Mobile Call Progress
廣告