Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

第89章:系統服務之 SMS 服務——寫一封有溫度的群發短信(從零開始學 Android)

編輯:Android 技術基礎

每逢佳節,電信三雄大概都在偷偷笑——又不知道有多少短信費進了口袋。儘管微信、QQ、飛信滿天飛,節日祝福短信的人氣卻始終擋不住。但千篇一律的群發實在沒什麼意義,你是不是也想要來個既個性又批量發送的花招?

想像一下:「老夫子同學,你好!祝你愚人節快樂!」——按分類從聯繫人中取出對象,再動態拼上名字與稱呼,這樣的短信是不是更有溫度、也更與眾不同?

發送短信的關鍵,在於 SmsManager 物件的 sendTextMessage() 方法。這個方法需要傳入五個參數,依序是:收件人地址(String)、發送位址(String)、發送服務(PendingIntent)與送達服務(PendingIntent)。其中,收件人與短信正文這兩個參數不可為 null。

一、設計界面

1. 佈局檔

打開 res/layout/activity_main.xml,安排輸入手機號碼、短信內容,以及發送按鈕:

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

    <TextView
        android:id="@+id/textphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手機號碼:" />

    <EditText
        android:id="@+id/editphone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textcont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="短信內容:" />

    <EditText
        android:id="@+id/editcont"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="發送" />

</LinearLayout>

二、程式檔

打開 src/com.genwoxue.sms/MainActivity.java:

package com.genwoxue.sms;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import android.app.PendingIntent;

public class MainActivity extends Activity {

    private EditText editPhone=null;
    private EditText editCont=null;
    private Button btnSend=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editPhone=(EditText)super.findViewById(R.id.editphone);
        editCont=(EditText)super.findViewById(R.id.editcont);

        btnSend=(Button)super.findViewById(R.id.send);
        btnSend.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                //實例化PendingIntent
                PendingIntent intent=PendingIntent.getActivity(
                    MainActivity.this,
                    0,
                    MainActivity.this.getIntent(),
                    PendingIntent.FLAG_UPDATE_CURRENT);

                //獲取SmsManager服務
                SmsManager sms=SmsManager.getDefault();
                //發送短信
                sms.sendTextMessage(
                    editPhone.getText().toString(), //手機號碼
                    "13800371500",                  //短信中心號碼
                    editCont.getText().toString(),  //內容
                    intent,
                    null);
            }
        });
    }
}

三、設定檔

打開 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.genwoxue.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.genwoxue.sms.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

別忘了加上發送短信的權限,否則程式執行到一半就會被系統攔下:

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

四、運行結果

輸入號碼與內容、按下發送,短信便會送往指定對象。實作進階一點,你完全可以串接聯繫人清單,讓每一則祝福都帶上對方的名字與稱呼。

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