編輯:開發入門
對一支手機來說,能撥打電話是最重要也最常用的功能。那麼在 Android 中該如何實作撥號程式呢?以下用一個簡單的 Demo,分五個步驟帶你完成。
新增一個 Android 專案,命名為 phoneCallDemo。
打開 main.xml,放入一個提示文字、一個輸入號碼的 EditText 與一個撥號按鈕:
<?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 phoneNumber:"
/>
<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" 會讓輸入框自動切換成電話鍵盤並限制可輸入字元,比自己做輸入驗證省事。
要直接撥號,必須在 AndroidManifest.xml 中宣告 CALL_PHONE 權限,否則程式會在執行時被系統拒絕:
<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>
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();
}
}
});
}
}
程式的關鍵是 Intent 的 action 與 data:action 使用 android.intent.action.CALL 表示直接撥出,data 則是以 tel: 開頭的 URI。系統收到這個意圖後,會自動找到對應的撥號程式完成撥號,這正是 Android 鬆耦合架構的體現。
實務上要注意:ACTION_CALL 會直接撥出,屬於高風險行為,因此除了宣告權限,較新的 Android 版本還需要執行期動態請求權限。若你只想把號碼帶到撥號面板由使用者確認,可以改用 Intent.ACTION_DIAL,那樣就不需要 CALL_PHONE 權限。



動手打造 Android RSS 閱讀器(三):把資料呈現在螢幕上
在 Android 中呈現 RSS 資料 此時,RSS 訂閱源裡的 XML 資料已安全地保存在記憶體中的 RSSFeed 實例裡,這個實例用一個便利的 List 結構裝著
Lotus iNotes Ultra-light 模式與重定向器新功能(一)
簡介:IBM Lotus iNotes 8.5 帶來了許多新特性。本系列第三篇要介紹的是全新的 Lotus iNotes ultra-light 模式,以及 Lotus
用 Eclipse 開發 Android 應用程式(二):安裝 SDK 與打造 SaySomething 應用
Android Software Developer Kit 我們已經大致了解 Android 平台,現在來談談如何把 Eclipse 環境配置得能支援 Android
用 Android、Scala 與 Eclipse 打造行動應用:以單位轉換器為例
先決條件 本文將帶你打造一個運行於 Android 裝置上的行動應用。你需要安裝 Android SDK,本文採用 V1.5 SDK。應用程式碼將以 Scala 語言撰寫