您的位置:首页 > 运维架构

群发短信并监控成功与否

2015-08-12 12:29 477 查看
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)
4000
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);

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: