編輯:關於 Android 開發
想在 App 裡加一個撥號功能,Android 提供了兩條路: ACTION_CALL 直接撥出,ACTION_DIAL 則是開啟系統撥號盤並帶入號碼,由使用者決定是否按下通話鍵。
要在 AndroidManifest.xml 中宣告撥號權限,這一步很容易漏掉:
<uses-permission android:name="android.permission.CALL_PHONE" />
一個輸入框加上兩個按鈕,分別對應兩種方式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="請輸入要撥打的號碼:" />
<EditText
android:id="@+id/etPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickActionCall"
android:text="ACTION_CALL 方式直接撥打" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickActionDial"
android:text="ACTION_DIAL 方式打開撥號界面" />
</LinearLayout>
package chengyujia.androidtest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class CallActivity extends Activity {
private EditText etPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
etPhone = (EditText) findViewById(R.id.etPhone);
}
// ACTION_CALL:直接撥打
public void onClickActionCall(View v) {
// ACTION_CALL 實際就是字串 "android.intent.action.CALL"
call(Intent.ACTION_CALL);
}
// ACTION_DIAL:開啟撥號界面
public void onClickActionDial(View v) {
// ACTION_DIAL 實際就是字串 "android.intent.action.DIAL"
call(Intent.ACTION_DIAL);
}
private void call(String action) {
String phone = etPhone.getText().toString();
if (phone != null && phone.trim().length() > 0) {
// "tel:" + 號碼 是固定格式,系統看到 tel: 開頭就知道後面是電話號碼
Intent intent = new Intent(action, Uri.parse("tel:" + phone.trim()));
startActivity(intent);
} else {
Toast.makeText(this, "電話號碼不能為空", Toast.LENGTH_LONG).show();
}
}
}
輸入 10086 後點擊第一顆按鈕:

並沒有直接撥出,而是跳出確認提示:

這是系統刻意的安全設計——防止程式在背景任意撥打付費電話。按下「允許一次」後才會真正撥號:

再回來測第二顆按鈕,ACTION_DIAL 直接開啟系統撥號盤:

號碼已經替你填好,要不要撥由使用者決定。
若追求操作步驟最少、路徑最短,用 ACTION_CALL;若希望把決定權交給使用者、避免誤觸爭議,用 ACTION_DIAL。另外提醒:Android 6.0 以上採取執行期權限機制,使用 ACTION_CALL 前必須先動態向使用者請求 CALL_PHONE 權限,否則會被系統攔下。
最後放個段子換換心情:
菩提老祖將悟空喚至身前:「你已學會長生不老術和七十二變,今日為師欲傳授你新的法術。」悟空道:「是何法術?」菩提老祖道:「看到這天上的雲彩了嗎?這邊有七朵雲彩,那邊有五朵雲彩,一共有幾朵?」悟空答:「十二朵。」菩提老祖道:「嗯,我要教你的就是雲計算。」
Android Fragment靜態加載完全指南
Android Fragment靜態加載完全指南 1. Fragment知識概要 自Android 3.0起,系統引入了Fragment,主要目的是為了在大螢幕裝置上實
打造下拉反彈效果:自定義 BounceScrollView 還原朋友圈封面交互
微信朋友圈頂部的封面圖、QQ 空間說說的頭圖,都有一個共同的交互細節:往下拉時露出背景圖,鬆手後有彈性回彈的動畫。這種「阻尼拉伸 + 回彈」的手感,用 ScrollVie
Android Activity 啟動模式:singleTop 行為與範例
Android Activity 啟動模式:singleTop 行為與範例singleTop 模式的特色是:如果目標 Activity 已經位於任務棧頂端,再次啟動它時不
從 Binder 到 AIDL:Android 跨進程通信的完整入門
從 Binder 到 AIDL:Android 跨進程通信的完整入門 在 Android 系統裡,應用程式與系統服務往往運行在不同進程中。為了兼顧安全性、穩定性與資源管理