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

Android 實作:打造撥打電話與傳送簡訊的應用

編輯:關於 Android 程式設計

本文以一個簡單的「打電話、發短信」範例應用為主軸,帶大家複習 Android 的四大核心元件、Activity 生命週期,以及 Intent、SmsManager 等常用 API 的實際用法。

Android 核心元件速覽

  • View:負責介面與 UI 控件的組織。
  • Intent:意圖物件,負責元件之間的通訊。
  • Activity:處理介面與使用者互動。
  • Content Provider:資料的儲存與共享。
  • Broadcast Receiver:接收訊息與事件處理。
  • Service:背景服務,例如與硬體、驅動相關的工作。
  • Notification:訊息與通知。

整個應用程式的入口由 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: } } }

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