android发送短信并监听插入收件箱的方法

package com.sms.service;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.*;
import android.net.Uri;
import android.telephony.SmsManager;
import org.apache.commons.lang3.RandomUtils;

import java.util.List;

/**
 * Created by Administrator on 2015/8/18.
 */
public class SmsService {
    //移动,每小时70,一天500
    //
    //电信 每小时200,一天1000
    private static final SmsService instance = new SmsService();
    public static final SmsService getInstance() {
        return instance;
    }
    public int send(final String phone, String message, final Context context) {
        if (message != null && phone != null) {
            SmsManager sms = SmsManager.getDefault();
            final List<String> texts = sms.divideMessage(message);

            PendingIntent sendIntent = null;
            PendingIntent backIntent = null;
            //处理返回的发送状态
            String SENT_SMS_ACTION = "SENT_SMS_ACTION";
            Intent sentIntent = new Intent(SENT_SMS_ACTION);
            for(int i=1;i<texts.size()+1;i++) {
                try {
                    sentIntent.putExtra(i + "", texts.get(i - 1));
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            sendIntent = PendingIntent.getBroadcast(context, phone.hashCode(), sentIntent, 0);
            //下标
            final StringBuffer index = new StringBuffer("0");
            context.registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context _context, Intent intent) {
                    index.append(Integer.parseInt(index.substring(index.length() - 1))+1);
                    switch (getResultCode()) {
                        case Activity.RESULT_OK:
                            String text = intent.getStringExtra(index.substring(index.length() - 1));
                            if (text == null){
                                break;
                            }
                            ContentValues values = new ContentValues();
                            values.put("date", System.currentTimeMillis());
                            values.put("read", 0);
                            values.put("type", 2);
                            values.put("address", phone);
                            values.put("body", text);
                            System.out.println("text:"+text);
                            context.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            break;
                    }
                    if (Integer.parseInt(index.substring(index.length() - 1)) == texts.size()){
                        context.unregisterReceiver(this);
                    }
                }
            }, new IntentFilter(SENT_SMS_ACTION));

            for(int i=0;i<texts.size();i++) {
                try {
                    //发送短信
                    sms.sendTextMessage(phone, null, texts.get(i), sendIntent, backIntent);
                    Thread.sleep(RandomUtils.nextLong(100, 600));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return texts.size();
        }
        return 0;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值