Android发送短信,并监听短信发送后是否发送成功的实现方法

废话不说,直接上源码:

package Service;

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.os.Handler;
import android.os.Message;
import android.telephony.gsm.SmsManager;
import android.widget.Toast;

import com.xxip.sendms.SendService;

import java.util.List;

/**
 * Created by dyb on 13-10-25.
 */
public class SendMessage {
	private static final String TAG = "SendMsg";
	private String SENT_SMS_ACTION = "SENT_SMS_ACTION";
	private Context context;
	private Intent sentIntent = new Intent(SENT_SMS_ACTION);
	private SmsManager smsManager;

	private PendingIntent sentPI;
	private String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
	private Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
	private PendingIntent deliverPI;
	//SendService myServer; //此为我应用中的服务,你可以取消
	private String qid = "0"; //短信序列号ID,该参数为我的应用使用的参数,你可以不要
	/**
	 * 构造函数
	 * @param c
	 */
	public SendMessage(Context c){
		this.context = c;
		smsManager = SmsManager.getDefault();
		sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0);


		//短信发送状态监控
		context.registerReceiver(new BroadcastReceiver(){
			@Override
			public void onReceive(Context context, Intent intent) {
				switch(getResultCode()){
					case Activity.RESULT_OK:
						MyLog.i(TAG,"信息已发出");
						updateStatus(qid,"1");
						break;
					case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
						Toast.makeText(context, "未指定失败 \n 信息未发出,请重试", Toast.LENGTH_LONG).show();
						break;
					case SmsManager.RESULT_ERROR_RADIO_OFF:
						Toast.makeText(context, "无线连接关闭 \n 信息未发出,请重试", Toast.LENGTH_LONG).show();
						break;
					case SmsManager.RESULT_ERROR_NULL_PDU:
						Toast.makeText(context, "PDU失败 \n 信息未发出,请重试", Toast.LENGTH_LONG).show();
						break;
				}

			}
		}, new IntentFilter(SENT_SMS_ACTION));

		//短信是否被接收状态监控
		deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0);
		context.registerReceiver(new BroadcastReceiver(){

			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				//myDialog.setMessage("已送达服务终端");
				//checkService();
			}
		}, new IntentFilter(DELIVERED_SMS_ACTION));


	}

	/**
	 * 发送短信,这里是我需要的几个参数,你可以根据你的具体情况来使用不同的参数
	 * @param qid
	 * @param mobile 要发送的目标手机号,这个必须要有
	 * @param code
	 * @param msg 发送的短信内容
	 */
	public void send(String qid,String mobile,String code,String msg){
		this.qid = qid;
		String msg1 = "尊敬的客户,您正在进行";
		String msg2 = "(6小时内有效),我站工作人员不会向您索取短信内容。[带你玩新乡]";
		msg1 += msg + "操作,短信授权码为";
		String content = msg1 + code + msg2;
		List<String> divideContents = smsManager.divideMessage(content);
		for(String text : divideContents){
			try{
				smsManager.sendTextMessage(mobile, "+8613010761500", text, sentPI, deliverPI);
			}catch(Exception e){
				Toast.makeText(this.context, "短信发送失败,请检查是系统否限制本应用发送短信", 5000).show();
				e.printStackTrace();
			}
		}
	}

	private void updateStatus(String qid, String status) {
		//短信发送成功后做什么事情,就自己定吧

	}


}

具体如何使用就很简单了,先实例化,然后调用 send方法即可。本例 中发送成功后要做的事情都在该类中完成了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值