提供一个带列表的对话框供用户选择群发短信的收件人号码,程序则使用了一个ArrayList<String>集合来保存所有的收件人的号码
为了实现群发功能,程序循环遍历ArrayList中的号码,依次向每个号码发送短信即可。
public class MainActivity extends Activity { EditText numbers, content; Button select, send; SmsManager smsManager; // 记录列表 ArrayList<String> sendlist = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); smsManager = SmsManager.getDefault(); numbers = (EditText) findViewById(R.id.numbers); content = (EditText) findViewById(R.id.content); select = (Button) findViewById(R.id.select); send = (Button) findViewById(R.id.send); send.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { // 一个一个去发送短信 for (String number : sendlist) { PendingIntent pi = PendingIntent.getActivity( MainActivity.this, 0, new Intent(), 0); smsManager.sendTextMessage(number, null, content.getText() .toString(), pi, null); } Toast.makeText(MainActivity.this, "短信群发完成", 8000).show(); } }); // 选择联系人 select.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 查询联系人的电话号码 // ContentResolver.query(Uri uri, String[] projection, String // selection, String[] selectionArgs, String sortOrder) final Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { // TODO Auto-generated method stub return cursor.getCount(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { cursor.moveToPosition(position); CheckBox rb = new CheckBox(MainActivity.this); // 获取联系人的电话号码,并去掉中间的中划线、空格 String number = cursor .getString( cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER)) .replace("-", "").replace(" ", ""); rb.setText(number); if (isChecked(number)) { rb.setChecked(true); } return rb; } }; // 加载list.xml布局文件对应的view界面 View selectView = getLayoutInflater().inflate(R.layout.list, null); // 获取selectView中名为list的ListView组件 final ListView listView = (ListView) selectView .findViewById(R.id.list); listView.setAdapter(adapter); new AlertDialog.Builder(MainActivity.this) .setView(selectView) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 清空 sendlist.clear(); // 遍历listView组件的每个列表项 for (int i = 0; i < listView.getCount(); i++) { CheckBox checkBox = (CheckBox) listView .getChildAt(i); // 如果该列表项被勾选 if (checkBox.isChecked()) { // 添加该列表项的电话号码 sendlist.add(checkBox.getText() .toString()); } } numbers.setText(sendlist.toString()); } }).show(); } }); } // 判断某个号码是否已在群发范围内 public boolean isChecked(String phone) { for (String s1 : sendlist) { if (s1.equals(phone)) { return true; } } return false; } }