編輯:關於 Android 程式設計
撥打電話在 Android 中是透過 Intent 系統完成的,本質上是把號碼包裝成 URI,交給系統選擇合適的應用來處理。這個範例示範一個輸入號碼後按下按鈕即可撥號的小程式。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SharpPhoneActivity!</string>
<string name="app_name">SharpPhone</string>
<string name="mobile">請輸入手機號</string>
<string name="button">撥打此號</string>
</resources>
一個提示文字、一個號碼輸入框、一顆按鈕,結構非常單純。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile" />
<EditText
android:id="@+id/phoneno"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
</LinearLayout>
按下按鈕時取出輸入框的號碼,做一次簡單的內容檢查,然後建立撥號 Intent 並啟動。
package com.sharpanroid.phone;
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;
public class SharpPhoneActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button) this.findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText phoneText = (EditText) findViewById(R.id.phoneno);
String phoneno = phoneText.getText().toString();
if (null != phoneno && !"".equals(phoneno.trim())) {
// 以 tel: 協定包裝號碼,建立撥號意圖
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:" + phoneno));
startActivity(intent);
}
}
});
}
}
程式碼中那個空值與空字串檢查看似多餘,實則必要。若直接送出空號碼,系統會開啟撥號程式卻沒有號碼,或者直接拋出例外,都是不好的體驗。
因為呼叫了撥號服務,必須在 AndroidManifest.xml 中申請 CALL_PHONE 權限,否則會得到 SecurityException。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.sharpanroid.phone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharpPhoneActivity"
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>
</manifest>
Android 中與電話相關的 Intent 主要有兩種,選錯會直接影響使用者體驗,也影響上架審核:
CALL_PHONE 這個高風險權限,Google Play 對此類權限的審查相當嚴格,一般應用不建議使用。若只是要提供「聯絡我們」這類功能,強烈建議改用 Intent.ACTION_DIAL,把 Uri.parse("tel:" + phoneno) 傳入即可,程式碼幾乎一樣,但省下權限申請與審核的麻煩。
另外,較新版本的 Android 在執行期需要動態請求 CALL_PHONE 權限,僅在清單中宣告是不夠的。使用模擬器測試時,若沒有預先設定模擬器支援通話,撥號動作可能只會停在撥號介面而不會真的接通。
Android Handler 異步通信機制詳解:Looper、MessageQueue 與訊息循環
為什麼需要 Handler 在 Android 中,UI 更新必須在主執行緒完成,但實際開發時經常需要從子執行緒取得資料、執行網路請求或讀取檔案。為了讓背景任務的結果能安
Android 仿微信:ViewPager 搭配頂部與底部菜單欄
本文實作一個仿微信風格的主介面:上方標題欄、中間可左右滑動的 ViewPager、下方四個 Tab 按鈕。用 ViewPager 承載內容,每一頁對應一個 XML;手指左
libGDX 跨平台遊戲開發入門:打造你的第一款 Android 遊戲
近年來,越來越多的跨平台遊戲框架相繼出現,填補了這個領域的空白,而移動遊戲平台的不斷成長與多樣化也讓市場日益蓬勃。如今,要挑一款遊戲引擎並開發你的第一款遊戲,比以往任何時
用 jarsigner 給 APK 簽名:參數說明與完整操作步驟
App 功能做完、準備上架時,常會遇到需要重新簽名的情況。最典型的是「認領」:公司裡其他同事已經在應用商店提交過同款 App,你要接手維護,商店會讓你下載一個空殼 APK