码迷,mamicode.com
首页 > 移动开发 > 详细

【Android】如何实现Android发送短信

时间:2017-11-13 14:08:05      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:XML   供应商   extends   def   radio   result   ack   管理器   receive   

第一种:调用系统短信接口直接发送短信;主要代码如下:

/** 
     * 直接调用短信接口发短信 
     * @param phoneNumber 
     * @param message 
     */  
public void sendSMS(String phoneNumber,String message){  
        //获取短信管理器   
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();  
        smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliverPI);    
    } 

第二种:如果短信的长度超过了发送了发送限制,那么可以使用如下的方法:

    /** 
     * 直接调用短信接口发短信 
     * @param phoneNumber 
     * @param message 
     */  
    public void sendMSM(String phoneNumber, String message) {
        android.telephony.SmsManager smsManager = android.telephony.SmsManager
                .getDefault();
        ArrayList<String> divideContents = smsManager.divideMessage(message);
        ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliverIntents = new ArrayList<PendingIntent>();
        for (String text : divideContents) {
            sentIntents.add(sentPI);
            deliverIntents.add(deliverPI);
        }
        smsManager.sendMultipartTextMessage(phoneNumber, null, divideContents,
                sentIntents, deliverIntents);
    }

 

别忘了权限,记得在AndroidMannifest.xml中设置权限:
<uses-permission android:name="android.permission.SEND_SMS" />

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

 

这种方法可以监控发送状态和对方接收状态。

处理返回的发送状态:

    //处理返回的发送状态   
    String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
    Intent sentIntent = new Intent(SENT_SMS_ACTION);  
    PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,  
            0);  
    // register the Broadcast Receivers  
    context.registerReceiver(new BroadcastReceiver() {  
        @Override  
        public void onReceive(Context _context, Intent _intent) {  
            switch (getResultCode()) {  
            case Activity.RESULT_OK:  
                Toast.makeText(context,  
            "短信发送成功", Toast.LENGTH_SHORT)  
            .show();  
            break;  
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
            break;  
            case SmsManager.RESULT_ERROR_RADIO_OFF:  
            break;  
            case SmsManager.RESULT_ERROR_NULL_PDU:  
            break;  
            }  
        }  
    }, new IntentFilter(SENT_SMS_ACTION));  

 

处理返回的接收状态 :

    //处理返回的接收状态   
    String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  
    // create the deilverIntent parameter  
    Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);  
    PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,  
           deliverIntent, 0);  
    context.registerReceiver(new BroadcastReceiver() {  
       @Override  
       public void onReceive(Context _context, Intent _intent) {  
           Toast.makeText(context,  
      "收信人已经成功接收", Toast.LENGTH_SHORT)  
      .show();  
       }  
    }, new IntentFilter(DELIVERED_SMS_ACTION));  

 

发送短信参数说明:

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

 

-- destinationAddress:目标电话号码
-- scAddress:短信中心号码,测试可以不填
-- text: 短信内容
-- sentIntent:发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理   即,这个意图包装了短信发送状态的信息
-- deliveryIntent: 发送 -->中国移动 --> 中国移动发送成功 --> 返回对方是否收到这个信息 --> 后续处理  即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。

 

接下来是一个Demo,android版本为4.4

首页界面:

技术分享

AndroidManifest.xml文件:

技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smstest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.SEND_SMS"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.smstest.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>
androidManifest.xml

MainActivity.java

技术分享
package com.example.smstest;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private LinearLayout llt;
    private Context context;
    private int PHONENUMBER=1001;
    private int SMSCONTENT=1002;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        llt= (LinearLayout)findViewById(R.id.main_layout);
        context=this;
         addText("电话号码");
        addTextField(PHONENUMBER);
        addText("短信内容");
        addTextField(SMSCONTENT);
        Button btn= addBtn("发送短信");
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                //处理返回的发送状态   
                String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
                Intent sentIntent = new Intent(SENT_SMS_ACTION);  
                PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent,  
                        0);  
                // register the Broadcast Receivers  
                context.registerReceiver(new BroadcastReceiver() {  
                    @Override  
                    public void onReceive(Context _context, Intent _intent) {  
                        switch (getResultCode()) {  
                        case Activity.RESULT_OK:  
                            Toast.makeText(context,  
                        "短信发送成功", Toast.LENGTH_SHORT)  
                        .show();  
                        break;  
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
                        break;  
                        case SmsManager.RESULT_ERROR_RADIO_OFF:  
                        break;  
                        case SmsManager.RESULT_ERROR_NULL_PDU:  
                        break;  
                        }  
                    }  
                }, new IntentFilter(SENT_SMS_ACTION));  
              //处理返回的接收状态   
                String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  
                // create the deilverIntent parameter  
                Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);  
                PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0,  
                       deliverIntent, 0);  
                context.registerReceiver(new BroadcastReceiver() {  
                   @Override  
                   public void onReceive(Context _context, Intent _intent) {  
                       Toast.makeText(context,  
                  "收信人已经成功接收", Toast.LENGTH_SHORT)  
                  .show();  
                   }  
                }, new IntentFilter(DELIVERED_SMS_ACTION)); 
                //获取短信手机号码
                EditText ett= (EditText)llt.findViewById(PHONENUMBER);
                String phoneNumber= ett.getText().toString();
                //获得短信信息
                EditText smsEtt=(EditText)llt.findViewById(SMSCONTENT);
                String smscontent=smsEtt.getText().toString();
                //创建一个短信管理器
                android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
                ArrayList<String> divideContents= smsManager.divideMessage(smscontent);
                ArrayList<PendingIntent> sentIntents =  new ArrayList<PendingIntent>();  
                ArrayList<PendingIntent> deliverIntents =  new ArrayList<PendingIntent>();  
                for(String text : divideContents){
                    sentIntents.add(sentPI);
                    deliverIntents.add(deliverPI);
                }
                smsManager.sendMultipartTextMessage(phoneNumber, null, divideContents, sentIntents, deliverIntents);  
            }
        });
    }

    Button addBtn(String text){
        Button btn=new Button(context);
        btn.setText(text);
        llt.addView(btn);
        return btn;
    }
    
    void addTextField(int id){
        EditText ett=new EditText(context);
        ett.setId(id);
        llt.addView(ett);
    }
    void addText(String text){
        TextView tv=new TextView(context);
        tv.setText(text);
        llt.addView(tv);
    }
}
MainActivity.java

 

【Android】如何实现Android发送短信

标签:XML   供应商   extends   def   radio   result   ack   管理器   receive   

原文地址:http://www.cnblogs.com/HDK2016/p/7825377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!