群发短信并监控成功与否

package com.example.contacttest;


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.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {


Context context;
PendingIntent sendIntent;
PendingIntent backIntent;
String []tel={"",""};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=getApplicationContext();
//处理返回的发送状态 
String SENT_SMS_ACTION = "SENT_SMS_ACTION";
Intent sentIntent = new Intent(SENT_SMS_ACTION);
sendIntent= 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);
backIntent= 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));

Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
for(int i=0;i<tel.length;i++)
{
sendSMS(tel[i], "test");
}
}
});

}

/**
     * 调用短信接口发短信,含接收报告和发送报告
     * 
     * @param phoneNumber
     * @param message
     */
    public void sendSMS(String phoneNumber, String message) {
        // 获取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
        // 拆分短信内容(手机短信长度限制)
        List<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            smsManager.sendTextMessage(phoneNumber, null, text, sendIntent, backIntent);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值