先注册一个broadcast:
private BroadcastReceiver smsBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent)
{
System.out.println("ACTION_RAYCLEAR_SEND_SMS");
if("ACTION_RAYCLEAR_SEND_SMS_test".equals(intent.getAction()))
{
boolean issucess = false;
switch (getResultCode()) {
case RESULT_OK:
issucess = true;
System.out.println("发送成功");
break;
default:
issucess = false;
System.out.println("发送失败");
break;
}
String phoneString = intent.getStringExtra("KEY_CONTACT_PHONE");
System.out.println(phoneString);
}
}
};
在onclick函数里群发短信
case R.id.main_start:
sendBroadcast(new Intent());
SmsManager smsManager = SmsManager.getDefault();
for(int i=0;i<5;i++)
{
System.out.println("send");
Intent sendintent = new Intent("ACTION_RAYCLEAR_SEND_SMS_test");
sendintent.putExtra("KEY_CONTACT_PHONE", String.format("1380000000%d", i));
smsManager.sendTextMessage(String.format("1380000000%d", i), null, "test", PendingIntent.getBroadcast(this, 0, sendintent, PendingIntent.FLAG_UPDATE_CURRENT), null);
}
break;
broadcast通过获取intent里附加的extradata来区分短信发送给谁
注意参数PendingIntent.FLAG_UPDATE_CURRENT,网上基本都是0,必须要用这个。