編輯:Android 開發實例
做一個能輸入號碼並直接撥出的簡單 App,是理解 Intent 跨元件溝通的好練習。以下拆解三個檔案。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="eoe.phone"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".MainActivity"
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="7" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
<uses-permission android:name="android.permission.CALL_PHONE"/> 這行不能漏。少了它,程式在執行撥號時會直接拋出 SecurityException 而崩潰。這個權限屬於危險等級,Android 6.0 以後還需要在執行期動態向使用者申請。
<?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="請輸入電話號碼" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拔打此號碼"
android:id="@+id/button" />
</LinearLayout>
package com.ljq.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 MainActivity extends Activity {
private EditText phone = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phone = (EditText) this.findViewById(R.id.phone);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String tel = phone.getText().toString();
// 方法一:使用 Intent,目的為激活 android 元件
// Intent intent = new Intent();
// intent.setAction("android.intent.action.CALL");
// intent.setData(Uri.parse("tel:" + tel));
// 方法二
Intent intent = new Intent("android.intent.action.CALL",
Uri.parse("tel:" + tel));
// 該方法內部會自動為 intent 對象設置類別:
// android.intent.category.DEFAULT
startActivity(intent);
}
});
}
}
程式碼中示範了兩種等價的 Intent 建構方式:先用空建構子再分別設定 action 與 data,或是直接用雙參數建構子一次完成。兩者結果完全相同,後者較簡潔。
但真正該思考的是用 CALL 還是 ACTION_DIAL:
CALL_PHONE 權限,且必須通過執行期權限申請。使用者沒有最後確認的機會。除非你的 App 本身就是電話應用(例如企業內部的客服系統),否則建議使用 ACTION_DIAL。Google Play 對使用 CALL_PHONE 權限的審核相當嚴格,若無法清楚說明必要性,上架會被退回。
另外補充兩個實務細節:輸入框可加上 android:inputType="phone" 讓鍵盤直接跳數字鍵盤;送出前應檢查號碼是否為空或格式不合法,避免帶著空字串去啟動撥號器。
重力感應入門:用加速度感測器讀取三軸數值
Android 裡很多遊戲都用到重力感應,值得花點時間理解它的運作方式。 先建立座標觀念 以螢幕左下角為原點,箭頭指向為正方向,範圍從 -10 到 10,以浮點數為刻度
Java 開發環境建置(二):寫出第一個 Hello World
JDK 裝好之後,用一支最經典的程式確認環境可用。建立一個名為 hello.java 的檔案: public class hello { public stat
Android 多語系支援:values 資料夾與語言切換
要讓 Android App 支援多國語言,最常見的方式是建立不同語言的 values 資源資料夾。這樣即使同一套程式碼,也能依裝置語言顯示不同文字。 資源資料夾命名規則
用 Mono for Android 實作 Android 4.0 高效導航(Effective Navigation)
Android 4.0 這一代系統在設計規範中明確定義了一套「高效導航」(Effective Navigation)模式,主要包含標籤頁、下拉清單,以及向上(Up)與返回