Android提供了短信群发的机制

来源:互联网 发布:淘宝店铺设计 编辑:程序博客网 时间:2024/05/16 06:59

短信群发与短信单发的区别在于群发时同一个内容可以对应多个目标手机号码,而单发一次只能有一个目标手机号码;当然我们也可用使用单发的形式来实现短信的群发,这样做的结果可能就是所发的短信条数比较多,还有就是比较麻烦;其实Android提供了短信群发的机制,在这里我们就以代码一的形式来说明一下android下短信群发实现,代码如下:

[java] view plain copy
  1. import java.util.HashSet;  
  2. import java.util.Set;  
  3. import android.app.Activity;  
  4. import android.app.PendingIntent;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.ContentValues;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.content.IntentFilter;  
  10. import android.net.Uri;  
  11. import android.os.Bundle;  
  12. import android.telephony.SmsManager;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.EditText;  
  17. import android.widget.Toast;  
  18.   
  19. /** 
  20.  * 短信群发 并将数据保存到数据库 
  21.  * */  
  22. public class SmsActity extends Activity {  
  23.     String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
  24.     String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";  
  25.     public static final Uri SMS_URI = Uri.parse("content://sms/");  
  26.     private EditText edit_no;  
  27.     private EditText edit_body;  
  28.     private Button button;  
  29.     private String body;// 短信内容  
  30.     private String[] address; //  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.sms_send);  
  36.         edit_no = (EditText) findViewById(R.id.phone);  
  37.         edit_body = (EditText) findViewById(R.id.body);  
  38.         button = (Button) findViewById(R.id.b_send);  
  39.   
  40.         button.setOnClickListener(new OnClickListener() {  
  41.             @Override  
  42.             public void onClick(View v) {  
  43.                 String p = edit_no.getText().toString();  
  44.                 body = edit_body.getText().toString();  
  45.                 // 不同电话号码用,隔开 如果是中文,请修改否则无法拆分  
  46.                 address = p.split(",");   
  47.                 Set<String> addr = new HashSet<String>();  
  48.                 for (int i = 0; i < address.length; i++) {  
  49.                     addr.add(address);  
  50.                 }  
  51.                 // 通过这个方法往SMs中插入数据时threads表也会更新  
  52.                 long id = Threads.getOrCreateThreadId(SmsActity.this, addr);  
  53.                 sendSMS(addr, body, id);  
  54.                 edit_no.setText("");  
  55.                 edit_body.setText("");  
  56.             }  
  57.         });  
  58.     }  
  59.   
  60.     public void sendSMS(Set<String> phone, String body, long threadId) {  
  61.         SmsManager msg = SmsManager.getDefault();  
  62.         Intent send = new Intent(SENT_SMS_ACTION);  
  63.         // 短信发送广播  
  64.         PendingIntent sendPI = PendingIntent.getBroadcast(this0, send, 0);  
  65.         Intent delive = new Intent(DELIVERED_SMS_ACTION);  
  66.         // 发送结果广播  
  67.         PendingIntent deliverPI = PendingIntent  
  68.                 .getBroadcast(this0, delive, 0);  
  69.         // 将数据插入数据库  
  70.         ContentValues cv = new ContentValues();  
  71.         for (String pno : phone) {  
  72.             msg.sendTextMessage(pno, null, body, sendPI, deliverPI);  
  73.             cv.put("thread_id", threadId);  
  74.             cv.put("date", System.currentTimeMillis());  
  75.             cv.put("body", body);  
  76.             cv.put("read"0);  
  77.             cv.put("type"2);  
  78.             cv.put("address", pno);  
  79.             this.getContentResolver().insert(SMS_URI, cv);  
  80.         }  
  81.   
  82.     }  
  83.   
  84.     protected void onResume() {  
  85.         super.onResume();  
  86.         // 注册监听  
  87.         registerReceiver(sendMessage, new IntentFilter(SENT_SMS_ACTION));  
  88.     }  
  89.   
  90.     BroadcastReceiver sendMessage = new BroadcastReceiver() {  
  91.         @Override  
  92.         public void onReceive(Context c, Intent intent) {  
  93.             // 判断短信是否成功  
  94.             switch (getResultCode()) {  
  95.             case Activity.RESULT_OK:  
  96.                 Toast.makeText(SmsActity.this"发送成功!", Toast.LENGTH_SHORT).show();  
  97.                 break;  
  98.             default:  
  99.                 Toast.makeText(SmsActity.this"发送失败!", Toast.LENGTH_SHORT).show();  
  100.                 break;  
  101.             }  
  102.         }  
  103.     };  

原创粉丝点击