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

Android 發送短信完整教學:從字串資源、布局到 SmsManager

編輯:關於 Android 程式設計

發送短信是許多 App 的基礎能力。本文以一個名為 lession01_sms 的專案為例,從字串資源、版面配置一路寫到 Java 邏輯,示範如何用 SmsManager 完成文字短信的發送。

一、字串資源 strings.xml

先把畫面上會用到的文字抽出來集中管理,這是好習慣,也方便日後做多語系:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">lession01_sms</string>
    <string name="action_settings">Settings</string>
    <string name="phone_num">請輸入手機號碼</string>
    <string name="phone_content">請輸入發送內容</string>
    <string name="phone_button">發送短信</string>

</resources>

二、布局檔 activity_sms.xml

版面採用 RelativeLayout,依序放入號碼輸入框、內容輸入框與發送按鈕,並透過 layout_below 讓它們垂直排列:

<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=".SmsActivity" >

    <TextView
        android:id="@+id/view_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_num" />
    <EditText
        android:id="@+id/edit_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:layout_below="@+id/view_phone"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/view_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_content"
        android:layout_below="@+id/edit_num" />
    <EditText
        android:id="@+id/edit_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:minLines="5"
        android:layout_below="@+id/view_content"/>
    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_button"
        android:layout_below="@+id/edit_content"/>

</RelativeLayout>

版面完成後,別忘了在 AndroidManifest.xml 中加入 SEND_SMS 權限,否則程式執行到發送環節會被系統攔下。

三、Java 邏輯 SmsActivity.java

核心流程是:取得號碼與內容 → 取得 SmsManager → 以 divideMessage 分頁 → 逐段送出 → 用 Toast 提示:

package com.example.lession01_sms;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SmsActivity extends Activity {

    // 聲明控件對象
    public Button btn_send;
    public EditText edit_num;
    public EditText edit_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 設置顯示的視圖(布局文件)
        setContentView(R.layout.activity_sms);
        // 獲取控件對象
        btn_send = (Button) findViewById(R.id.button_send);
        edit_num = (EditText) findViewById(R.id.edit_num);
        edit_content = (EditText) findViewById(R.id.edit_content);

        // 為按鈕控件注冊事件
        btn_send.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // 獲取電話號碼
                String phone_num = edit_num.getText().toString();
                // 獲取發送內容
                String phone_content = edit_content.getText().toString();
                // 獲取短信管理器對象
                SmsManager smsManager = SmsManager.getDefault();
                // 意圖對象
                PendingIntent pendingIntent = PendingIntent.getBroadcast(
                        SmsActivity.this, 0, new Intent(), 0);

                // 70 分頁
                ArrayList<String> contents = smsManager.divideMessage(phone_content);
                for (String c : contents) {
                    smsManager.sendTextMessage(phone_num, null, c, pendingIntent, null);
                }

                // 發送信息操作
                smsManager.sendTextMessage(phone_num, null, phone_content,
                        pendingIntent, null);
                // 吐司效果
                Toast.makeText(SmsActivity.this, "發送完畢", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sms, menu);
        return true;
    }

}

其中 divideMessage 會依電信單則短信的長度上限把過長內容切片,再以迴圈分批送出,避免因超出長度而發送失敗。PendingIntent 則用於接收發送結果的回呼。

以下是執行過程的畫面:

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