編輯: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 動態索取權限,否則依然會失敗;三是撥打前跳一個確認對話框,是避免誤觸造成實際通話的常見做法。
Android手機衛士(十六):事件傳遞與 CheckBox 點擊衝突
當點擊 SettingItemView 時,事件會由父層配置接收,再傳遞給自訂元件。若點擊位置在 CheckBox,事件可能被 CheckBox 消耗,導致自訂元件無法更
Android 進度提示:用 ProgressBar 做一個轉不停的小圈圈
當 App 要下載檔案、讀取資料庫或等待網路回應時,使用者需要一個「它還在動」的訊號,否則很容易以為程式當掉了。Android 的 ProgressBar 就是為這件事而
Android miniTwitter 登入畫面實作:漸層背景、圓角表單與相對佈局
登入畫面是許多 App 的第一印象。本文以 miniTwitter 登入頁為例,說明如何用漸層背景、圓角容器、相對佈局與表單控制項,做出一個清爽美觀的登入介面。 畫面結構
用 Android Canvas 手繪南丁格爾玫瑰圖(Nightingale Rose Diagram)
第一次看到南丁格爾玫瑰圖時,我在常規圖表裡實在感到驚艷——但老實說,第一眼沒看懂。查了背景才知道這位女士有多麼了不起,確實值得尊敬。 仔細研究之後發現,這種圖的構成原理其