直奔主题~!
结构如图:
person_content.java代码:
public class person_content extends ListActivity { private Button submit_result_btn; private HashMapal; @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); CheckBox cb = (CheckBox) v.findViewById(R.id.cb); TextView username_txt = (TextView) v.findViewById(R.id.username); TextView phonenum_txt = (TextView) v.findViewById(R.id.tel_num); String username = username_txt.getText().toString(); String phonenum = phonenum_txt.getText().toString(); cb.setEnabled(true); if (cb.isChecked()) { cb.setChecked(false); al.remove(phonenum); } else { cb.setChecked(true); al.put(phonenum, username); } cb.setEnabled(false); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.show_contacts); bind(); } public void bind() { al = new HashMap (); submit_result_btn = (Button) this.findViewById(R.id.submit_result_btn); Cursor ps_cs = getContentResolver().query(People.CONTENT_URI, null, null, null, null); startManagingCursor(ps_cs); String[] string = { Phones.NAME, Phones.NUMBER }; int[] intText = { R.id.username, R.id.tel_num }; SimpleCursorAdapter sca = new SimpleCursorAdapter(person_content.this, R.layout.person_content, ps_cs, string, intText); setListAdapter(sca); submit_result_btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent it = new Intent(); Bundle bd = new Bundle(); bd.putSerializable("select_people", al); it.putExtras(bd); setResult(1, it); finish(); } }); }}
Sms_Send_AllActivity.java代码:
public class Sms_Send_AllActivity extends Activity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub // super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { if (resultCode == 1) { Bundle bd = data.getExtras(); al = (HashMap) bd .getSerializable("select_people"); } } } private Button select_btn; private Button send_btn; private EditText content_txt; private HashMap al; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findAll(); bind(); } public void findAll() { select_btn = (Button) this.findViewById(R.id.People_btn); send_btn = (Button) this.findViewById(R.id.submit_btn); content_txt = (EditText) this.findViewById(R.id.content_txt); } public void bind() { select_btn.setOnClickListener(mylistener); send_btn.setOnClickListener(mylistener); } private View.OnClickListener mylistener = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.People_btn: Intent it = new Intent(Sms_Send_AllActivity.this, person_content.class); startActivityForResult(it, 0); break; case R.id.submit_btn: String content = content_txt.getText().toString(); SmsManager sm = SmsManager.getDefault(); for (Iterator iter = al.entrySet().iterator(); iter.hasNext();) { Map.Entry element = (Entry) iter.next(); String number = element.getKey().toString(); // 这句目的是为了在模拟器上真实模拟 number = number.replace("-", "").trim(); String name = element.getValue().toString(); PendingIntent sentIntent = PendingIntent.getBroadcast( Sms_Send_AllActivity.this, 0, new Intent(), 0); sm.sendTextMessage(number, null, content, sentIntent, null); } Toast.makeText(Sms_Send_AllActivity.this, "发送成功", Toast.LENGTH_LONG).show(); break; default: break; } } };}
main.xml代码:
person_content.xml代码:
show_contacts.xml代码:
androidManifest.xml代码: