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

Android 開發系列(二):動手做一個短信發送器

編輯:關於 Android 程式設計

本文的目標很明確:做一個能用的短信發送器,包含收件號碼輸入、正文輸入和發送按鈕。

界面效果大致如下:

一、申明發送短信的權限

發送短信屬於敏感操作,必須先在 AndroidManifest.xml 中申明權限,否則運行時會直接拋出安全異常:

<uses-permission android:name="android.permission.SEND_SMS"/>

二、佈局文件 main.xml

兩個輸入框(號碼、內容)加一個按鈕,正文框用 minLines="3" 撐高一點,方便輸入多行文本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="@string/number"
        android:id="@+id/numberlabel" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/number"
        android:layout_toRightOf="@id/numberlabel"
        android:layout_alignTop="@id/numberlabel"
        android:layout_marginLeft="5dp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/content" />

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minLines="3"
        android:id="@+id/content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button" />

</LinearLayout>

三、Java 代碼實現發送

核心只用了一個類:SmsManager。

package cn.itcast.sms;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText numberText;
    private EditText contentText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        numberText = (EditText) this.findViewById(R.id.number);
        contentText = (EditText) this.findViewById(R.id.content);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());
    }

    private final class ButtonClickListener implements View.OnClickListener {
        public void onClick(View v) {
            String number = numberText.getText().toString();
            String content = contentText.getText().toString();

            // 取得默認的短信管理器
            SmsManager manager = SmsManager.getDefault();

            // 長短信需要拆分,否則超出長度的部分會發送失敗
            ArrayList<String> texts = manager.divideMessage(content);
            for (String text : texts) {
                manager.sendTextMessage(number, null, text, null, null);
            }

            Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG).show();
        }
    }
}

四、關鍵點說明

  • SmsManager.getDefault() 拿到系統默認的短信管理器。
  • divideMessage() 這一步不能省。短信有長度限制,超過限制的消息如果不拆分,會直接發送失敗。拆分後逐條發送,接收方會自動拼接。
  • sendTextMessage() 的後兩個參數分別是「發送結果的 PendingIntent」和「送達狀態的 PendingIntent」。這裡傳 null 表示不關心結果;如果需要向用戶反饋發送成功與否,就應該在這裡傳入 PendingIntent 並配合廣播接收器處理。

一個實務提醒:這個 Demo 是「直接由系統發送」,在用戶那邊會產生真實的短信記錄與費用。如果是要做正式的產品功能,更合適的方案是調起系統短信界面(Intent.ACTION_SENDTO),讓用戶自己確認後再發送,既符合用戶預期,也能避開較新系統上的權限與政策審核問題。

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