文字来源:http://www.glmei.cn/?p=3464
代码: 全选
//短信发送API解释
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent);
/**
* 参数解释
* destinationAddress:收信人的手机号码
* scAddress:发信人的手机号码
* text:发送信息的内容
* sentIntent:发送是否成功的回执,用于监听短信是否发送成功。
* DeliveryIntent:接收是否成功的回执,用于监听短信对方是否接收成功。
*/
//短信群发
for (int i = 0; i < contactList.size(); i ) {
Intent itSend = new Intent(SENT_SMS_ACTION);
itSend.putExtra(KEY_PHONENUM, contactList.get(i));
PendingIntent mSendPI = PendingIntent.getBroadcast(getApplicationContext(), i/××requestCode××/, itSend, PendingIntent.FLAG_ONE_SHOT/××flag××/);//这里requestCode和flag的设置很重要,影响数据KEY_PHONENUM的传递。
String content = mContext.getString(R.string.test);
smsManager.sendTextMessage(contactList.get(i), null, content, mSendPI, null);
}
public static final String SENT_SMS_ACTION = "SENT_SMS_ACTION";
private SMSSendResultReceiver mSMSReceiver = new SMSSendResultReceiver();
private IntentFilter mSMSResultFilter = new IntentFilter();
//广播注册
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSMSFilter.addAction(SENT_SMS_ACTION);
registerReceiver(mSMSReceiver, mSMSResultFilter);
...
}
//广播定义
class SMSSendResultReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String phoneNum = intent.getStringExtra(KEY_PHONENUM);
// TODO Auto-generated method stub
switch(getResultCode())
{
case Activity.RESULT_OK:
System.out.println("Send Message to " phoneNum " success!");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_RADIO_OFF:
case SmsManager.RESULT_ERROR_NULL_PDU:
default:
System.err.println("Send Message to " phoneNum " fail!");
break;
}
}
}