SmsManager类发送短信

利用类SmsManager 发送信息, smsManager 为 SmsManager 一个默认的实例. SmsManager smsManager = SmsManager.getDefault();

  smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)  

  destinationAddress: 收件人号码 

  scAddress: 短信中心服务号码, 这里设置为null 

  text: 发送内容

  sentIntent: 发送短信结果状态信号(是否成功发送),new 一个Intent , 操作系统接收到信号后将广播这个Intent.此过程为异步.

  deliveryIntent: 对方接收状态信号(是否已成功接收).

  由于需要用到系统发送信息功能, 要在AndroidMainfest.xml 加入 <uses-permission android: name="android.permisson.SEND_SMS" />

 

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.PhoneSMS.melody"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PhoneSMSActivity"
                  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>
    <uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>

 

1.界面设计 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- 编辑接收号码 -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/textSMSTo"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:lines="3" android:hint="@string/edtSMSTo"
        android:id="@+id/edtSMSTo"/>
    <!-- 编辑发送内容 -->
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/textContent"/>
    <EditText android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:lines="3"
        android:hint="@string/edtContent" android:id="@+id/edtContent"/>
    <!-- 发送 -->
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/btnSent"
        android:id="@+id/btnSent"/>

</LinearLayout>

 

2.字符变量 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, PhoneSMSActivity!</string>
    <string name="app_name">PhoneSMS</string>
    <string name="textSMSTo">收件人</string>
    <string name="edtSMSTo">收件人号码</string>
    <string name="textContent">发送内容</string>
    <string name="edtContent">输入内容</string>
    <string name="btnSent">发送</string>
</resources>

 

3.Activity代码编写

PhoneSMSActivity.java

package com.PhoneSMS.melody;

import java.util.List;

import android.app.Activity;
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.widget.Toast;

public class PhoneSMSActivity extends Activity implements OnClickListener {
    private EditText edtSMSTo = null; // 收件人控件
    private EditText edtContent = null; // 发送内容控件
    private Button btnSent = null; // 发送btn 控件
    private String SMSTo = null; // 收件人号码
    private String SMSContent = null; // 发送内容
   

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /**
         * 获取控件
         */
        edtSMSTo = (EditText) this.findViewById(R.id.edtSMSTo);
        edtContent = (EditText) this.findViewById(R.id.edtContent);
        btnSent = (Button) this.findViewById(R.id.btnSent);

        // 获取收件人号码
        SMSTo = edtSMSTo.getText().toString();
        // 获取发送内容
        SMSContent = edtContent.getText().toString();
        // 设置监听事件
        btnSent.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnSent:
            /**
             * 实现短信发送功能
             */
            // 获取默认短信管理对象
            SmsManager smsManager = SmsManager.getDefault();
            // 判断发送内容字数(一件信息最多70 字)
            if(SMSContent.length() <= 70) {
                smsManager.sendTextMessage(SMSTo, null, SMSContent, null, null);
            }else{
                // SmsManger 类中 divideMessage 会将信息按每70 字分割
                List<String> smsDivs = smsManager.divideMessage(SMSContent);
                for(String sms : smsDivs) {
                    smsManager.sendTextMessage(SMSTo, null, sms, null, null);
                   
                }
            }
            Toast.makeText(PhoneSMSActivity.this, "信息已发送", Toast.LENGTH_SHORT);
            break;
        }
    }
}

你可能感兴趣的:(SmsManager类发送短信)