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

安卓實作撥打電話:Intent 撥號與 CALL_PHONE 權限設定

編輯:Android 開發實例

打電話是手機最基礎的能力,在 Android 裡實作起來卻有兩條路,差別在要不要「直接撥出」。用 ACTION_DIAL 只是把號碼帶進撥號盤讓使用者按確認,不需要任何權限;用 ACTION_CALL 則會直接撥打,必須在 Manifest 宣告 CALL_PHONE 權限,而且屬於危險權限等級,正式環境還要向使用者動態請求。本範例示範的是後者。

步驟一:建立專案

新增一個 Android 專案,命名為 phoneCallDemo。

步驟二:設計介面

打開 main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Please input the phoneNumer:"

/>

<EditText

android:id="@+id/et1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:phoneNumber="true"

/>

<Button

android:id="@+id/bt1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Call Phone"

/>

</LinearLayout>

android:phoneNumber="true" 這個屬性會讓輸入框自動切換成電話鍵盤,並套用數字輸入的過濾規則,比 android:inputType 更早期的寫法,現在建議改用 inputType="phone"。

步驟三:宣告應用基本資訊

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="https://schemas.android.com/apk/res/android"

package="com.android.test"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".PhoneCallDemo"

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>

<uses-sdk android:minSdkVersion="3" />

步驟四:加上撥號權限

<uses-permission android:name="android.permission.CALL_PHONE">

</uses-permission>

</manifest>

少了這一行,startActivity() 會直接拋出 SecurityException,這是初學者最常遇到的失敗原因之一。

步驟五:主程式

package com.android.test;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class PhoneCallDemo extends Activity {

private Button bt;

private EditText et;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//取得資源

bt = (Button)findViewById(R.id.bt1);

et = (EditText)findViewById(R.id.et1);

//增加事件響應

bt.setOnClickListener(new Button.OnClickListener(){

@Override

public void onClick(View v) {

//取得輸入的電話號碼串

String inputStr = et.getText().toString();

//如果輸入不為空創建打電話的Intent

if(inputStr.trim().length()!=0)

{

Intent phoneIntent = new Intent("android.intent.action.CALL",

Uri.parse("tel:" + inputStr));

//啟動

startActivity(phoneIntent);

}

//否則Toast提示一下

else{

Toast.makeText(PhoneCallDemo.this, "不能輸入為空", Toast.LENGTH_LONG).show();

}

}

});

}

}

整段程式碼的精髓只有一行:Uri.parse("tel:" + inputStr)。Android 用 URI 的 scheme 來判斷意圖,tel: 代表電話,mailto: 代表郵件,smsto: 代表簡訊,都是同一套邏輯。把 action 換成 ACTION_DIAL 就是「只開撥號盤不撥出」,換成 ACTION_VIEW 則會讓系統跳出選擇視窗。

實務上有三點建議:一是記得對輸入內容做 trim() 並過濾非數字字元,避免 Uri 解析異常;二是若採用 ACTION_CALL,在 Android 6.0 以上必須用 checkSelfPermission 與 requestPermissions 動態索取權限,否則依然會失敗;三是撥打前跳一個確認對話框,是避免誤觸造成實際通話的常見做法。

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