編輯:關於 Android 程式設計
「撥打電話」看似簡單,卻牽涉到意圖(Intent)、URI 以及權限三個環節。本文以 lession01_phone 專案為例,說明如何讓使用者輸入號碼後,按下按鈕即可直接撥出。
先建立撥打電話專案,版面概念很單純:一個提示文字、一個號碼輸入框、一顆撥打按鈕。
使用 RelativeLayout,透過 layout_below 讓輸入框與按鈕依序排在提示文字下方,並用 inputType="phone" 叫出電話鍵盤:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhoneActivity" >
<TextView
android:id="@+id/viewphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone" />
<EditText
android:id="@+id/editphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/viewphone"
android:layout_below="@+id/viewphone"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btnphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editphone"
android:layout_below="@+id/editphone"
android:layout_marginTop="23dp"
android:text="@string/buttonphone" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">lession01_phone</string>
<string name="action_settings">Settings</string>
<string name="phone">撥打電話</string>
<string name="buttonphone">撥打</string>
</resources>
同樣地,記得在 AndroidManifest.xml 中加入 CALL_PHONE 權限,否則 ACTION_CALL 無法直接撥號。
關鍵在於建立一個帶有 tel: 前綴 URI 的 ACTION_CALL 意圖,把它交給 startActivity 後,系統就會進入撥號流程:
package com.example.lession01_phone;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PhoneActivity extends Activity {
private Button button_phone;
private EditText edit_phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone);
button_phone = (Button) findViewById(R.id.btnphone);
edit_phone = (EditText) findViewById(R.id.editphone);
button_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone_Num = edit_phone.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone_Num));
PhoneActivity.this.startActivity(intent);
Toast.makeText(PhoneActivity.this, "正在給" + phone_Num + "撥打", Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.phone, menu);
return true;
}
}
補充一個實務上的選擇:若改用 Intent.ACTION_DIAL,系統只會跳轉到撥號盤並帶入號碼,由使用者自行按下撥打,這種方式不需要 CALL_PHONE 權限,對使用者也較友善;而 ACTION_CALL 則是直接撥出,屬於較敏感的操作。
執行成果如下:
Android draw9patch(.9 圖)製作與使用詳解
.9 圖(NinePatch)是 Android 平台上特有的一種可拉伸圖片格式,廣泛應用於按鈕、對話框等需要自適應大小的背景。掌握它,能讓你的界面在不同分辨率下都保持美
Android ProgressBar 客製化:旋轉動畫與自訂水平進度條
ProgressBar 是少數「預設樣式很醜、但客製化很簡單」的元件。本文示範兩種常見需求:換掉那個轉圈圈的小動畫,以及做出一條自訂配色的水平進度條。 一、自訂旋轉類型的
Android 游戏实战:打飞机游戏怪物(敌机)类实现(四)
分析:依据敌机类型区分敌机的运动逻辑与绘制方式。 一、Enemy 敌机类 敌机类通过 type 标识区分不同种类,并为每种敌机赋予各自的移动速度与 AI 逻辑:
Android 廣播、服務、數據庫、通知、包等核心術語圖解介紹
簡介 Android 系統運用了許多機制,例如廣播、服務、數據庫、通知、包等等。本文將逐一介紹這些核心術語的概念,並搭配示意圖幫助理解。 什麼是廣播?Broadcast