Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實戰教學:動手做一個電話撥號器

編輯:關於 Android 開發

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)後重新執行,撥號功能就正常了:

這個小案例提醒我們:凡是涉及系統敏感功能的操作,別忘了先在清單檔案中申請對應的權限。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved