編輯:關於 Android 程式設計
本文以一個簡單的「打電話、發短信」範例應用為主軸,帶大家複習 Android 的四大核心元件、Activity 生命週期,以及 Intent、SmsManager 等常用 API 的實際用法。
整個應用程式的入口由 AndroidManifest.xml 定義,而 R 類別則是 aapt 工具依資源檔自動產生的索引檔,用來存放各種資源的 ID。
首先在 activity_main.xml 中,用一個 RelativeLayout 排列出輸入框、多個撥號按鈕與簡訊輸入框。以號碼輸入框為例:
<EditText android:id="@+id/NummberText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textColor="#FF0099" android:ems="10" android:hint="@string/SMS_please" android:inputType="phone"> <requestFocus /> </EditText>
其中 android:inputType="phone" 會自動帶出電話鍵盤,android:hint 則是未輸入時顯示的提示文字。文字內容建議抽到 strings.xml 中統一管理,方便日後維護與多語系支援。
讓 MainActivity 實作 OnClickListener 介面,就能用一個統一的 onClick 來管理所有按鈕事件。撥打電話的關鍵在於建立 Intent、設定 ACTION_CALL 動作,並以 tel: 協定帶入號碼:
private void MyCallOther(){ String num = Nummber_edit.getText().toString(); if(TextUtils.isEmpty(num)){ Toast.makeText(MainActivity.this, "電話為空了", 0).show(); return ; }else{ Intent intent = new Intent(); Toast.makeText(MainActivity.this, "電話打向"+num, 0).show(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+num)); startActivity(intent); return; } }
傳送簡訊則需使用 SmsManager。先確認號碼與內容不為空,再呼叫 sendTextMessage:
private void SendFriend(){ String number = Nummber_edit.getText().toString(); String sms = Talk_Edit.getText().toString(); if(TextUtils.isEmpty(number)||TextUtils.isEmpty(sms)){ Toast.makeText(MainActivity.this, "信息為空了", 0).show(); return ; }else{ SmsManager message = SmsManager.getDefault(); message.sendTextMessage(number, null, sms, null, null); Toast.makeText(MainActivity.this, "信息發送成功", 0).show(); } }
在 onClick 中透過 switch(v.getId()) 比對被點擊的元件 ID,呼叫對應的方法;同時可設計一個 ClearEdi() 方法,在操作完成後清空輸入框:
@Override public void onClick(View v) { switch(v.getId()){ case R.id.Call_otherButton : MyCallOther();ClearEdi(); break; case R.id.Call_xiaoyuButton : MyCallxiaoyu(); break; case R.id.Call_mangqi: CallWangqi ();ClearEdi(); break; case R.id.Send_friend: SendFriend();ClearEdi(); break; case R.id.Send_wangqi: SendWangqi();ClearEdi(); break; case R.id.Send_xiaoyu: SendXiaoyu();ClearEdi(); break; default: } }
撥打電話與傳送簡訊都屬於敏感操作,必須在 AndroidManifest.xml 中聲明 CALL_PHONE 與 SEND_SMS 等權限,否則程式執行時會因缺乏權限而失敗。
以下是這個範例程式的版面配置與 Java 程式碼,供讀者參考:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/NummberText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textColor="#FF0099" android:ems="10" android:hint="@string/SMS_please" android:inputType="phone"> <requestFocus /> </EditText> <Button android:id="@+id/Call_otherButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/NummberText" android:text="@string/call_other" /> <Button android:id="@+id/Call_xiaoyuButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/Call_otherButton" android:text="@string/love_xiaoyu" /> <Button android:id="@+id/Call_mangqi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/Call_xiaoyuButton" android:text="@string/call_wangqi" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:lines="8" android:hint="@string/_love" android:inputType="textMultiLine" /> </RelativeLayout>
package com.example.love_happy; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.telephony.gsm.SmsManager; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ Button Call_other; Button Call_wangqi; Button Call_xiaoyu; Button Send_other; Button Send_wangqi; Button Send_xiaoyu; EditText Nummber_edit; EditText Talk_Edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Nummber_edit = (EditText)findViewById(R.id.NummberText); Talk_Edit= (EditText)findViewById(R.id.editText1); Call_other = (Button)findViewById(R.id.Call_otherButton); Call_wangqi= (Button)findViewById(R.id.Call_mangqi); Call_xiaoyu= (Button)findViewById(R.id.Call_xiaoyuButton); Send_other= (Button)findViewById(R.id.Send_friend); Send_wangqi= (Button)findViewById(R.id.Send_wangqi); Send_xiaoyu= (Button)findViewById(R.id.Send_xiaoyu); Call_other.setOnClickListener(MainActivity.this); Call_wangqi.setOnClickListener(MainActivity.this); Call_xiaoyu.setOnClickListener(MainActivity.this); Send_other.setOnClickListener(MainActivity.this); Send_wangqi.setOnClickListener(MainActivity.this); Send_xiaoyu.setOnClickListener(MainActivity.this); Nummber_edit.setOnClickListener(MainActivity.this); Talk_Edit.setOnClickListener(MainActivity.this); } private void MyCallOther(){ String num = Nummber_edit.getText().toString(); if(TextUtils.isEmpty(num)){ Toast.makeText(MainActivity.this, "電話為空了", 0).show(); return ; }else{ Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+num)); startActivity(intent); return; } } private void SendFriend(){ String number = Nummber_edit.getText().toString(); String sms = Talk_Edit.getText().toString(); if(TextUtils.isEmpty(number)||TextUtils.isEmpty(sms)){ Toast.makeText(MainActivity.this, "信息為空了", 0).show(); return ; }else{ SmsManager message = SmsManager.getDefault(); message.sendTextMessage(number, null, sms, null, null); Toast.makeText(MainActivity.this, "信息發送成功", 0).show(); } } @Override public void onClick(View v) { switch(v.getId()){ case R.id.Call_otherButton : MyCallOther();break; case R.id.Send_friend: SendFriend();break; default: } } }


Android Service 綁定機制與跨進程溝通(下)
綁定服務概念 bindService 讓其他元件與 Service 建立更直接的連線。透過 ServiceConnection,客戶端可接收 onBind 回傳的 IBi
Mac 上下載、編譯與查看 Android 源碼
環境準備 在 Mac 上編譯與查看 Android 源碼,第一步是準備一個適合編譯的環境。由於 Android 源碼對檔案系統大小寫敏感,因此不建議直接在一般 Mac 檔
深入理解 Android 之 AOP:從 JoinPoint 到 AspectJ 落地實踐
一、閒談 AOP 大家都熟悉 OOP(Object Oriented Programming,物件導向)。本文要談的是 AOP(Aspect Oriented Progr
Android 事件處理機制總結(三):Handler 消息傳遞完整解析
一、Android 的三類事件處理機制 事件處理的本質是「響應使用者的介面操作,提升程式的互動性」。Android 提供了三套機制: 1. 基於監聽的事件處理——最直