編輯:關於 Android 開發
今天我們跟著影片步驟,從零開始打造一個簡單的 Android 應用——電話撥號器。這個小專案能幫助入門者熟悉介面佈局、按鈕事件以及 Intent 的使用,同時也是理解 Android 權限機制的好例子。
首先照慣例新建一個 Android 專案。

在 activity_main.xml 中先寫入最基本的元件:
<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=".MainActivity" >
<!-- 文字方塊 -->
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入號碼" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
執行後你會發現佈局出問題了:

文字發生了重疊。最簡單的解法是把 RelativeLayout 換成 LinearLayout,並設定垂直排列:
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical" >
<!-- 文字方塊 -->
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入號碼" />
<EditText android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/bt_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="撥打" />
</LinearLayout>
現在介面就整齊多了:

接著在 MainActivity 中為按鈕加上點擊事件,透過 Intent 告訴系統我們要撥打電話:
package com.wuyudong.dial;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 透過資源 id 取得佈局中的按鈕元件
Button bt = (Button) findViewById(R.id.bt_call);
// 為按鈕設定監聽
bt.setOnClickListener(new MyListener());
}
class MyListener implements OnClickListener {
@Override
public void onClick(View v) {
System.out.println("按鈕被點擊了!");
// 透過資源 id 取得輸入框
EditText et = (EditText) findViewById(R.id.et_number);
// 取得輸入框內容
String number = et.getText().toString().trim();
// 告訴系統我們的動作是打電話
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
// 啟動撥號應用
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
撥打電話時,程式卻崩潰並拋出安全異常:
java.lang.SecurityException: Permission Denial: starting Intent
{ act=android.intent.action.CALL dat=tel:xxx cmp=com.android.phone/.OutgoingCallBroadcaster }
... requires android.permission.CALL_PHONE
原因很明確:應用沒有取得撥號權限。解決方法是在 AndroidManifest.xml 中宣告 CALL_PHONE 權限。

點擊 Add,選擇 Users Permission:

在右側下拉清單中選擇 android.permission.CALL_PHONE:

存檔(Ctrl+S)後重新執行,撥號功能就正常了:

這個小案例提醒我們:凡是涉及系統敏感功能的操作,別忘了先在清單檔案中申請對應的權限。
一次伺服器流量異常的排查實錄
記錄一次伺服器異常外發流量的排查過程 前兩天接到一位做開發的朋友來電,說他們客戶的一台伺服器開機後,整個網段的機器上網都變慢了,連他遠程操作這台伺服器也一卡一卡的。我的第
Android 滾動機制入門:mScrollX、scrollTo 與重繪原理
Android 滾動機制入門:mScrollX、scrollTo 與重繪原理 在理解 Android 使用者介面的滾動效果之前,需要先釐清觸摸事件、座標系統與 View
Android 自定義 View 繪製流程重點整理
Android 自定義 View 繪製流程重點整理自定義 View 的測量、配置與繪製是理解 Android UI 的關鍵。若直接繼承 View 或 ViewGroup,
Android res 目錄結構詳解:各文件夾用途與資源引用方式
Android res 目錄結構與資源使用完全指南 1. 相關文件夾介紹 在 Android 項目中,主要資源文件放在 res 文件夾裡;assets 文件夾則存放不經編