短信群发与短信单发的区别在于群发时同一个内容可以对应多个目标手机号码,而单发一次只能有一个目标手机号码;当然我们也可用使用单发的形式来实现短信的群发,这样做的结果可能就是所发的短信条数比较多,还有就是比较麻烦;其实Android提供了短信群发的机制,在这里我们就以代码一的形式来说明一下android下短信群发实现,代码如下:
- import java.util.HashSet;
- import java.util.Set;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.BroadcastReceiver;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.Uri;
- import android.os.Bundle;
- import android.telephony.SmsManager;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * 短信群发 并将数据保存到数据库
- * */
- public class SmsActity extends Activity {
- String SENT_SMS_ACTION = "SENT_SMS_ACTION";
- String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
- public static final Uri SMS_URI = Uri.parse("content://sms/");
- private EditText edit_no;
- private EditText edit_body;
- private Button button;
- private String body;// 短信内容
- private String[] address; //
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sms_send);
- edit_no = (EditText) findViewById(R.id.phone);
- edit_body = (EditText) findViewById(R.id.body);
- button = (Button) findViewById(R.id.b_send);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String p = edit_no.getText().toString();
- body = edit_body.getText().toString();
- // 不同电话号码用,隔开 如果是中文,请修改否则无法拆分
- address = p.split(",");
- Set<String> addr = new HashSet<String>();
- for (int i = 0; i < address.length; i++) {
- addr.add(address);
- }
- // 通过这个方法往SMs中插入数据时threads表也会更新
- long id = Threads.getOrCreateThreadId(SmsActity.this, addr);
- sendSMS(addr, body, id);
- edit_no.setText("");
- edit_body.setText("");
- }
- });
- }
- public void sendSMS(Set<String> phone, String body, long threadId) {
- SmsManager msg = SmsManager.getDefault();
- Intent send = new Intent(SENT_SMS_ACTION);
- // 短信发送广播
- PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, send, 0);
- Intent delive = new Intent(DELIVERED_SMS_ACTION);
- // 发送结果广播
- PendingIntent deliverPI = PendingIntent
- .getBroadcast(this, 0, delive, 0);
- // 将数据插入数据库
- ContentValues cv = new ContentValues();
- for (String pno : phone) {
- msg.sendTextMessage(pno, null, body, sendPI, deliverPI);
- cv.put("thread_id", threadId);
- cv.put("date", System.currentTimeMillis());
- cv.put("body", body);
- cv.put("read", 0);
- cv.put("type", 2);
- cv.put("address", pno);
- this.getContentResolver().insert(SMS_URI, cv);
- }
- }
- protected void onResume() {
- super.onResume();
- // 注册监听
- registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));
- }
- BroadcastReceiver sendMessage = new BroadcastReceiver() {
- @Override
- public void onReceive(Context c, Intent intent) {
- // 判断短信是否成功
- switch (getResultCode()) {
- case Activity.RESULT_OK:
- Toast.makeText(SmsActity.this, "发送成功!", Toast.LENGTH_SHORT).show();
- break;
- default:
- Toast.makeText(SmsActity.this, "发送失败!", Toast.LENGTH_SHORT).show();
- break;
- }
- }
- };
- }