分析下怎么写

首先,我们需要一个输入框,可以手动的输入手机号码,

其次,很少有人愿意手动输入,那么我们需要提供一个按钮来给我们的用户选择自己电话本中的联系人(一次可以选择多个即群发)

然后,我们需要一个短信编辑界面,可以编辑短信

最后两个按钮,一个发送,点击后发送消息,一个取消(取消后存为草稿,目前没有开发)

这个是我的UI,当然很难看,后续会优化

先把布局文件放上来,就不多分析了,布局很简单,里面需要的图片就自己找个地方抠一下了  activity_newmessage.xml

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="50dp"
  4. android:background="@color/title"
  5. android:gravity="center" >
  6.  
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="@string/newmessage_tools"
  11. android:textColor="#ffffff"
  12. android:textSize="25dp" />
  13. </LinearLayout>
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="50dp"
  17. android:layout_weight="1"
  18. android:gravity="center_vertical">
  19. <EditText
  20. android:id="@+id/et_number"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:hint="@string/phonenumber_body"
  24. android:ems="10"
  25. android:layout_weight="3"
  26. android:inputType="phone" >
  27. <requestFocus />
  28. </EditText>
  29.  
  30. <Button
  31. android:id="@+id/bt_contact"
  32. android:layout_width="wrap_content"
  33. android:layout_height="40dp"
  34. android:layout_weight="1"
  35. android:background="@drawable/add_contact_selector"/>
  36. </LinearLayout>
  37. <EditText
  38. android:id="@+id/et_content"
  39. android:gravity="top"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_weight="8"
  43. android:hint="@string/message_body"/>
  44. <LinearLayout
  45. android:layout_width="match_parent"
  46. android:layout_height="50dp"
  47. android:gravity="center_vertical"
  48. android:layout_weight="1">
  49. <Button
  50. android:id="@+id/bt_send"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_weight="1"
  54. android:background="@drawable/button_selector"
  55. android:text="@string/send"/>
  56. <Button
  57. android:id="@+id/bt_cancel"
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_weight="1"
  61. android:background="@drawable/button_selector"
  62. android:text="@string/cancel"/>
  63. </LinearLayout>

再给大家一个按钮  selector吧button_selector

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true"
  4. android:drawable="@drawable/sms_add_contact_pressed" /> <!-- pressed -->
  5. <item android:state_focused="true"
  6. android:drawable="@drawable/sms_add_contact_pressed" /> <!-- focused -->
  7. <item android:drawable="@drawable/sms_add_contact_nomal" /> <!-- default -->
  8. </selector>

然后点击那个小人按钮,我们需要载入联系人如下图:

目前写了一个全部联系人,当然支持从手机联系人选择和sim卡选择问题也不大的

模拟器上联系人比较少。

选择联系人以后,点击确认,会自动将联系人填写到发送短信页面的电话号码那个EDITTEXT中

下面再把这个页面的布局放上来,

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="50dp"
  4. android:gravity="center"
  5. android:background="@color/title">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="@string/title_contact"
  10. android:textColor="#ffffff"
  11. android:textSize="25dp"/>
  12. </LinearLayout>
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="50dp"
  16. android:gravity="center_vertical"
  17. android:layout_weight="1">
  18. <Button
  19. android:id="@+id/bt_selectall"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_weight="1"
  23. android:background="@drawable/button_selector"
  24. android:text="@string/selectall"/>
  25. <Button
  26. android:id="@+id/bt_selectnone"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:background="@drawable/button_selector"
  31. android:text="@string/selectnone"/>
  32. </LinearLayout>
  33. <LinearLayout
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:layout_weight="4">
  37.  
  38. <ListView
  39. android:id="@+id/lv_contact"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content" />
  42. </LinearLayout>
  43. <LinearLayout
  44. android:layout_width="match_parent"
  45. android:layout_height="50dp"
  46. android:gravity="center_vertical"
  47. android:layout_weight="1">
  48. <Button
  49. android:id="@+id/bt_sure"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_weight="1"
  53. android:background="@drawable/button_selector"
  54. android:text="@string/sure"/>
  55. <Button
  56. android:id="@+id/bt_cl"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_weight="1"
  60. android:background="@drawable/button_selector"
  61. android:text="@string/cancel"/>
  62. </LinearLayout>

ok,界面的工作到此结束,接下来我们就要开始代码的编写了,新建消息页面

  1. package com.xiaoxu.message;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.PendingIntent;
  9. import android.content.BroadcastReceiver;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.Intent;
  13. import android.content.IntentFilter;
  14. import android.os.Bundle;
  15. import android.telephony.SmsManager;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.Window;
  19. import android.widget.Button;
  20. import android.widget.EditText;
  21. import android.widget.Toast;
  22.  
  23. import com.google.gson.Gson;
  24. import com.google.gson.reflect.TypeToken;
  25. import com.xiaoxu.message.bean.ContactInfo;
  26. import com.xiaoxu.message.util.SoundPoolButton;
  27.  
  28. public class NewMessageActivity extends Activity implements OnClickListener {
  29. private SoundPoolButton soundpool;
  30. private EditText numET;
  31. private EditText contentET;
  32. private Button addcontact,send,cancel;
  33. private List<ContactInfo> select;
  34.  
  35. /* 自定义ACTION常数,作为广播的Intent Filter识别常数 */
  36. private static String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN";
  37. private static String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
  38. private mServiceReceiver mReceiver01, mReceiver02;
  39.  
  40. protected void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. requestWindowFeature(Window.FEATURE_NO_TITLE);
  43. setContentView(R.layout.activity_newmessage);
  44. soundpool = new SoundPoolButton(this);
  45.  
  46. numET = (EditText) findViewById(R.id.et_number); // 获取2个文本框
  47. contentET = (EditText) findViewById(R.id.et_content);
  48.  
  49. addcontact = (Button) findViewById(R.id.bt_contact);
  50. send = (Button) findViewById(R.id.bt_send);
  51. cancel = (Button) findViewById(R.id.bt_cancel);
  52.  
  53. addcontact.setOnClickListener(this);
  54. send.setOnClickListener(this);
  55. cancel.setOnClickListener(this);
  56. }
  57.  
  58. private void send() {
  59. //获取到了所有联系人
  60. String num = numET.getText().toString().trim(); // 获取电话号码和短信内容
  61. String content = contentET.getText().toString();
  62. String number = "";
  63. // 截取联系人
  64. if(!num.endsWith(";")){
  65. num += ";";
  66. //拿一个号码,发一个号码
  67. number = num.substring(0, num.indexOf(";"));
  68. /* 建立SmsManager对象 */
  69. SmsManager smsManager = SmsManager.getDefault();
  70. /* 建立自定义Action常数的Intent(给PendingIntent参数之用) */
  71. Intent itSend = new Intent(SMS_SEND_ACTIOIN);
  72. Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
  73. /* sentIntent参数为传送后接受的广播信息PendingIntent */
  74. PendingIntent mSendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itSend, 0);
  75. /* deliveryIntent参数为送达后接受的广播信息PendingIntent */
  76. PendingIntent mDeliverPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itDeliver, 0);
  77. ArrayList<String> list = smsManager.divideMessage(content); // 使用短信管理器把短信分段
  78. if (list.size() == 0) {
  79. Toast.makeText(getApplicationContext(), R.string.content_empty, Toast.LENGTH_SHORT).show(); // 弹出通知
  80. return;
  81. }
  82. for (String sms : list) { // 逐段发送
  83. smsManager.sendTextMessage(number, null, sms, mSendPI, mDeliverPI); // 使用短信管理器发送指定内容到指定号码上
  84. }
  85. return;
  86. }
  87.  
  88. while(num.length()>0){
  89. //拿一个号码,发一个号码
  90. number = num.substring(0, num.indexOf(";"));
  91.  
  92. /* 建立SmsManager对象 */
  93. SmsManager smsManager = SmsManager.getDefault();
  94. /* 建立自定义Action常数的Intent(给PendingIntent参数之用) */
  95. Intent itSend = new Intent(SMS_SEND_ACTIOIN);
  96. // Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
  97. /* sentIntent参数为传送后接受的广播信息PendingIntent */
  98. PendingIntent mSendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itSend, 0);
  99.  
  100. /* deliveryIntent参数为送达后接受的广播信息PendingIntent */
  101. //暂时不使用,为了绕过系统提示发消息
  102. // PendingIntent mDeliverPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itDeliver, 0);
  103. // /* 发送SMS短信,注意倒数的两个PendingIntent参数 */
  104. // smsManager.sendTextMessage(number, null, content, mSendPI, mDeliverPI);
  105. //
  106. ArrayList<String> list = smsManager.divideMessage(content); // 使用短信管理器把短信分段
  107. if (list.size() == 0) {
  108. Toast.makeText(getApplicationContext(), R.string.content_empty, Toast.LENGTH_SHORT).show(); // 弹出通知
  109. return;
  110. }
  111. for (String sms : list) { // 逐段发送
  112. smsManager.sendTextMessage(number, null, sms, mSendPI, null); // 使用短信管理器发送指定内容到指定号码上
  113. }
  114.  
  115. if(num.length()>num.indexOf(";")+1){
  116. num = num.substring(num.indexOf(";")+1);
  117. }else{
  118. break;
  119. }
  120. }
  121. }
  122.  
  123. @Override
  124. public void onClick(View view) {
  125. soundpool.play();
  126. switch(view.getId()){
  127. case R.id.bt_contact:
  128. String[] items={"查看全部","查看SIM卡","查看手机"};
  129. new AlertDialog.Builder(this) //
  130. .setTitle("选择联系人") //
  131. .setCancelable(true) //
  132. .setItems(items, new DialogInterface.OnClickListener() {
  133. @Override
  134. public void onClick(DialogInterface dialog, int which) {
  135. switch(which){
  136. case 0:
  137. Intent intent = new Intent(NewMessageActivity.this,ContactActivity.class);
  138. startActivityForResult(intent, 0);
  139. break;
  140. case 1:break;
  141. case 2:break;
  142. }
  143. }
  144. }) // listener 为OnClickListener 监听器对象, 监听列表项被选中
  145. .show();
  146. break;
  147. case R.id.bt_send:
  148. send();
  149. //TODO 跳转到发送消息页面
  150. break;
  151. case R.id.bt_cancel:
  152. finish();
  153. break;
  154. }
  155.  
  156. }
  157. @Override
  158. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  159. if(data!=null){
  160. Gson gson = new Gson();
  161. // System.out.println(data.getStringExtra("return"));
  162. String str = data.getStringExtra("return");
  163. select = gson.fromJson(str, new TypeToken<ArrayList<ContactInfo>>(){}.getType());
  164. String number = new String();
  165. for(int i= 0;i<select.size();i++){
  166. if(number!=null&&number.length()>1){
  167. number += ";"+select.get(i).getUserNumber();
  168. }else{
  169. number = select.get(i).getUserNumber();
  170. }
  171. }
  172. number+=";";
  173. numET.setText(number);
  174. super.onActivityResult(requestCode, resultCode, data);
  175. }
  176. }
  177.  
  178. /**
  179. *
  180. * 自定义mServiceReceiver重写BroadcastReceiver监听短信状态信息
  181. *
  182. */
  183. public class mServiceReceiver extends BroadcastReceiver
  184. {
  185. @Override
  186. public void onReceive(Context context, Intent intent)
  187. {
  188. if (intent.getAction().equals(SMS_SEND_ACTIOIN))
  189. {
  190. try
  191. {
  192. switch(getResultCode())
  193. {
  194. case Activity.RESULT_OK:
  195. /* 发送短信成功 */
  196. Toast.makeText(getApplicationContext(), "发送成功", 0).show();
  197. contentET.setText("");
  198. break;
  199. case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
  200. /* 发送短信失败 */
  201. Toast.makeText(getApplicationContext(), "发送失败", 0).show();
  202. break;
  203. case SmsManager.RESULT_ERROR_RADIO_OFF:
  204. break;
  205. case SmsManager.RESULT_ERROR_NULL_PDU:
  206. break;
  207. }
  208. }
  209. catch(Exception e)
  210. {
  211. Toast.makeText(getApplicationContext(), "发送失败", 0).show();
  212. e.getStackTrace();
  213. }
  214. }
  215. //暂时未使用
  216. else if(intent.getAction().equals(SMS_DELIVERED_ACTION))
  217. {
  218. try
  219. {
  220. /* android.content.BroadcastReceiver.getResultCode()方法 */
  221. switch(getResultCode())
  222. {
  223. case Activity.RESULT_OK:
  224. /* 短信 */
  225. Toast.makeText(getApplicationContext(), "发送成功", 0).show();
  226. break;
  227. case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
  228. /* 短信未送达 */
  229. Toast.makeText(getApplicationContext(), "发送失败", 0).show();
  230. break;
  231. case SmsManager.RESULT_ERROR_RADIO_OFF:
  232. break;
  233. case SmsManager.RESULT_ERROR_NULL_PDU:
  234. break;
  235. }
  236. }
  237. catch(Exception e)
  238. {
  239. Toast.makeText(getApplicationContext(), "发送失败", 0).show();
  240. e.getStackTrace();
  241. }
  242. }
  243. }
  244. }
  245.  
  246. //这是重载Activity中的函数
  247. @Override
  248. protected void onResume()
  249. {
  250. /* 自定义IntentFilter为SENT_SMS_ACTIOIN Receiver */
  251. IntentFilter mFilter01;
  252. mFilter01 = new IntentFilter(SMS_SEND_ACTIOIN);
  253. mReceiver01 = new mServiceReceiver();
  254. registerReceiver(mReceiver01, mFilter01);
  255.  
  256. /* 自定义IntentFilter为DELIVERED_SMS_ACTION Receiver */
  257. mFilter01 = new IntentFilter(SMS_DELIVERED_ACTION);
  258. mReceiver02 = new mServiceReceiver();
  259. registerReceiver(mReceiver02, mFilter01);
  260.  
  261. super.onResume();
  262. }
  263.  
  264. @Override
  265. protected void onPause()
  266. {
  267. /* 取消注册自定义Receiver */
  268. unregisterReceiver(mReceiver01);
  269. unregisterReceiver(mReceiver02);
  270. super.onPause();
  271. }
  272.  
  273. }

注意点:

1:跳转到联系人页面的时候,我们使用startactivityforresult,这样我们可以将选择的联系人的信息带过来到这个页面。
2:发送短信

  1. smsManager.sendTextMessage(number, null, content, mSendPI, null);

这个方法,第一个参数是电话号码,第二个参数不管,第三个参数是短信内容,第四个参数是发送状态的广播接受者,第四个参数是对方是否接收到的接受者

还要注意 如果我们将第三个和第四个接收者都填写的话,android系统会跳出一个提示框,说应用正在发送短信,这个很不理想,所以我们只填写 mSendPI这个接收者

3:我们的广播接收者在当前页面定义就Ok了,但是在什么时候注册,什么时候取消注册是关键。我们只有当当前页面获取到了焦点之后才需要注册,在失去焦点时取消注册,就OK了

接下来是选择联系人页面代码

  1. package com.xiaoxu.message;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.database.Cursor;
  8. import android.os.Bundle;
  9. import android.provider.ContactsContract;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.widget.AdapterView;
  13. import android.widget.AdapterView.OnItemClickListener;
  14. import android.widget.Button;
  15. import android.widget.ListView;
  16.  
  17. import com.google.gson.Gson;
  18. import com.xiaoxu.message.adapter.ContactAdapter;
  19. import com.xiaoxu.message.bean.ContactInfo;
  20. import com.xiaoxu.message.util.SoundPoolButton;
  21.  
  22. public class ContactActivity extends Activity implements OnClickListener {
  23. private List<ContactInfo> contactlist;
  24. private ListView listview;
  25. private ContactAdapter adapter;
  26. private Button selectall,selectnone,sure,cancel;
  27. private SoundPoolButton soundpool;
  28.  
  29. /** Called when the activity is first created. */
  30. @Override
  31. public void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_contacts);
  34. init();
  35. }
  36.  
  37. private void init(){
  38. contactlist = new ArrayList<ContactInfo>();
  39. selectall = (Button) findViewById(R.id.bt_selectall);
  40. selectnone = (Button) findViewById(R.id.bt_selectnone);
  41. sure = (Button) findViewById(R.id.bt_sure);
  42. cancel = (Button) findViewById(R.id.bt_cl);
  43. listview = (ListView) findViewById(R.id.lv_contact);
  44. soundpool = new SoundPoolButton(this);
  45. adapter = new ContactAdapter(contactlist,this);
  46.  
  47. // new Thread(){
  48. // public void run() {
  49. getAllContacts();
  50. // };
  51. // }.start();
  52.  
  53. listview.setAdapter(adapter);
  54. listview.setOnItemClickListener(new OnItemClickListener() {
  55. @Override
  56. public void onItemClick(AdapterView<?> arg0, View view, int position,
  57. long arg3) {
  58. // TODO Auto-generated method stub
  59. }
  60. });
  61.  
  62. selectall.setOnClickListener(this);
  63. selectnone.setOnClickListener(this);
  64. sure.setOnClickListener(this);
  65. cancel.setOnClickListener(this);
  66. }
  67.  
  68. private void getAllContacts(){
  69. // 获得所有的联系人
  70. Cursor cur = getContentResolver().query(
  71. ContactsContract.Contacts.CONTENT_URI,
  72. null,null,null,
  73. ContactsContract.Contacts.DISPLAY_NAME
  74. + " COLLATE LOCALIZED ASC");
  75. // 循环遍历
  76. if (cur.moveToFirst()) {
  77. int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
  78. int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
  79. while (cur.moveToNext()){
  80. ContactInfo contact = new ContactInfo();
  81. // 获得联系人的ID号
  82. String contactId = cur.getString(idColumn);
  83. // 获得联系人姓名
  84. String disPlayName = cur.getString(displayNameColumn);
  85. contact.setContactName(disPlayName);
  86. // 查看该联系人有多少个电话号码。如果没有这返回值为0
  87. int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  88. if (phoneCount > 0) {
  89. // 获得联系人的电话号码
  90. Cursor phones = getContentResolver().query(
  91. ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  92. null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
  93. + " = " + contactId, null, null);
  94. if (phones.moveToFirst()) {
  95. do {
  96. // 遍历所有的电话号码
  97. String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  98. contact.setUserNumber(phoneNumber);
  99. } while (phones.moveToNext());
  100. }
  101. phones.close();
  102.  
  103. }
  104. contactlist.add(contact);
  105. }
  106. }
  107. cur.close();
  108. }
  109.  
  110. @Override
  111. public void onClick(View v) {
  112. Gson gson = new Gson();
  113. Intent intent = new Intent();
  114. soundpool.play();
  115. switch(v.getId()){
  116. case R.id.bt_selectall:
  117. for(int i=0;i<contactlist.size();i++){
  118. contactlist.get(i).setChecked(true);
  119. }
  120. adapter.notifyDataSetChanged();
  121. break;
  122. case R.id.bt_selectnone:
  123. for(int i=0;i<contactlist.size();i++){
  124. contactlist.get(i).setChecked(false);
  125. }
  126. adapter.notifyDataSetChanged();
  127. break;
  128. case R.id.bt_sure:
  129. ArrayList<ContactInfo> select = new ArrayList<ContactInfo>();
  130. for(int i=0;i<contactlist.size();i++){
  131. if(contactlist.get(i).getChecked()){
  132. select.add(contactlist.get(i));
  133. }
  134. }
  135. String str = gson.toJson(select);
  136. intent.putExtra("return", str);
  137. setResult(RESULT_OK, intent);
  138. finish();
  139. break;
  140. case R.id.bt_cl:
  141. setResult(RESULT_CANCELED, null);
  142. finish();
  143. break;
  144. }
  145.  
  146. }
  147. }

还有里面用到的适配器(因为比较懒,所以这个listview暂时还没有进行优化的,大家莫怪)

  1. package com.xiaoxu.message.adapter;
  2.  
  3. import java.util.List;
  4.  
  5. import android.content.Context;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.CheckBox;
  10. import android.widget.CompoundButton;
  11. import android.widget.CompoundButton.OnCheckedChangeListener;
  12. import android.widget.TextView;
  13.  
  14. import com.xiaoxu.message.R;
  15. import com.xiaoxu.message.bean.ContactInfo;
  16.  
  17. public class ContactAdapter extends BaseAdapter{
  18. private List<ContactInfo> contactlist;
  19. private Context context;
  20.  
  21. public ContactAdapter(List<ContactInfo> contactlist,Context context) {
  22. this.contactlist = contactlist;
  23. this.context = context;
  24. }
  25.  
  26. @Override
  27. public int getCount() {
  28. return contactlist.size();
  29. }
  30.  
  31. @Override
  32. public Object getItem(int position) {
  33. return contactlist.get(position);
  34. }
  35.  
  36. @Override
  37. public long getItemId(int position) {
  38. return position;
  39. }
  40.  
  41. @Override
  42. public View getView(final int position, View convertView, ViewGroup parent) {
  43. View view;
  44. if(convertView!=null){
  45. view = View.inflate(context,R.layout.contact_item, null);
  46. TextView name = (TextView) view.findViewById(R.id.tv_contact_item_name);
  47. TextView number = (TextView) view.findViewById(R.id.tv_contact_item_number);
  48. CheckBox cb = (CheckBox) view.findViewById(R.id.cb_check);
  49. ContactInfo contact = contactlist.get(position);
  50. name.setText(contact.getContactName());
  51. number.setText(contact.getUserNumber());
  52. cb.setChecked(contact.getChecked());
  53.  
  54. cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  55. @Override
  56. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  57. // TODO Auto-generated method stub
  58. contactlist.get(position).setChecked(isChecked);
  59. }
  60. });
  61. }else{
  62. view = View.inflate(context,R.layout.contact_item, null);
  63. TextView name = (TextView) view.findViewById(R.id.tv_contact_item_name);
  64. TextView number = (TextView) view.findViewById(R.id.tv_contact_item_number);
  65. CheckBox cb = (CheckBox) view.findViewById(R.id.cb_check);
  66. ContactInfo contact = contactlist.get(position);
  67. name.setText(contact.getContactName());
  68. number.setText(contact.getUserNumber());
  69. cb.setChecked(contact.getChecked());
  70. cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  71. @Override
  72. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  73. // TODO Auto-generated method stub
  74. contactlist.get(position).setChecked(isChecked);
  75. }
  76. });
  77. }
  78.  
  79. return view;
  80. }
  81.  
  82. }

这个就是选择联系人的页面,点击确定按钮则将联系人转化成json串当作字符串传过去

这里还有一个javabean,联系人信息

  1. package com.xiaoxu.message.bean;
  2.  
  3. public class ContactInfo {
  4. private String contactName;
  5. private String userNumber;
  6. private boolean isChecked;
  7.  
  8. public String getContactName() {
  9. return contactName;
  10. }
  11. public void setContactName(String contactName) {
  12. this.contactName = contactName;
  13. }
  14. public String getUserNumber() {
  15. return userNumber;
  16. }
  17. public void setUserNumber(String userNumber) {
  18. this.userNumber = userNumber;
  19. }
  20. public boolean getChecked() {
  21. return isChecked;
  22. }
  23. public void setChecked(boolean isChecked) {
  24. this.isChecked = isChecked;
  25. }
  26.  
  27. }

已经可以了,不妨试一下吧。

这里面不涉及什么难点,如果遇到了问题,欢迎提问吧

Android-->发送短信页面实现(短信发送以及群发和从电话本中选择联系人)-----------》2的更多相关文章

  1. 深度解析:Android在Mms设置页面更改短信中心号码流程

    相关控件初始化方法:showSmscPref private void showSmscPref() {         int count = MSimTelephonyManager.getDef ...

  2. andorid中发送短信页面以及邮件发送

    跳转到发送短信页面 Uri smsToUri = Uri.parse("smsto://10086"); Intent mIntent = new Intent( android. ...

  3. Android软件开发之发送短信与系统短信库解析

    今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...

  4. Android 短信模块分析(四) MMS之短信的发送与接收

     MMS之短信的发送与接收分析: 一.信息发送: com.android.mms.data.WorkingMessage.java 类 send()函数: public void send() { . ...

  5. Android系统应用Mms之Sms短信发送流程(Mms应用部分)二

    1. 新建一条短信, 在发送短信之前, 首先创建的是一个会话Conversation, 以后所有与该接收人(一个或多个接收人)的消息交互, 都在该会话Conversation中. ComposeMes ...

  6. android安全问题(八)伪造短信(利用原生android4.0漏洞)

    导读:本文利用android4.0的一个原生漏洞来伪造短信.无须声明任何权限即可伪造发送方为任何号码的短信给用户. android4.0发布已经是很久很久很久很久以前的事情了,这个漏洞早就报了出来,之 ...

  7. Android黑科技,读取用户短信+修改系统短信数据库

    安卓系统比起ios系统最大的缺点,相信大家都知道,就是系统安全问题.这篇博客就秀一波“黑科技”. 读取用户短信 Android应用能读取用户手机上的短信,相信已经不是什么新鲜事,比如我们收到的短信验证 ...

  8. Flask实战第42天:注册页面对接短信接口及接口加密

    我们来看下之前写的 sms_captcha函数 @bp.route('/sms_captcha/') def sms_captcha(): params = {'code':'abcd'} resul ...

  9. Java调用腾讯云短信接口,完成验证码的发送(不成功你来砍我!!)

    一.前言 我们在一些网站注册页面,经常会见到手机验证码的存在,这些验证码一般的小公司都是去买一些大的厂家的短信服务,自己开发对小公司的成本花费太大了!今天小编就带着大家来学习一下腾讯云的短信接口,体验 ...

随机推荐

  1. unix命令: ifconfig

    ifconfig 命令被用来: 1.为一个网卡分配一个IP地址 2.设置本地环路界面 3.分配一个子网掩码(可选) HP-UX: # /usr/sbin/ifconfig lan0 lan0: fla ...

  2. Python爬虫入门三之Urllib库的基本使用

    转自http://cuiqingcai.com/947.html 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由 ...

  3. QT对话框中show和exec的区别

    转自:http://hi.baidu.com/wangjuns8/blog/item/24b382460dd1c1338694737d.html QDialog的显示有两个函数show()和exec( ...

  4. GreenDAO数据库版本升级

    GreenDAO是一款非要流行的android平台上的数据库框架,性能优秀,代码简洁. 初始化数据库模型代码的时候需要使用java项目生成代码,依赖的jar包已经上传到我的资源里了,下载地址如下:ht ...

  5. 基于visual Studio2013解决C语言竞赛题之1080填运算符

        题目 解决代码及点评 /************************************************************************/ /* ...

  6. QNX系统-关于delay函数与sleep函数的区别

    QNX是类unix系统.在c语言编程过程中,往往会用到delay或者sleep延时函数.两者之间在使用上有一定的区别!!! delay()是循环等待,该进程还在运行,占用处理器. sleep()不同, ...

  7. java中完美打包

    前言: 我们都知道Java可以将二进制程序打包成可执行jar文件,双击这个jar和双击exe效果是一样一样的,但感觉还是不同.其实将java程序打包成exe也需要这个可执行jar文件. 准备: ecl ...

  8. BUG: scheduling while atomic: events/0/4/总结

    对于Linux内核来说,Oops就意外着内核出了异常,此时会将产生异常时CPU的状态,出错的指令地址.数据地址及其他寄存器,函数调用的顺序甚至是栈里面的内容都打印出来,然后根据异常的严重程度来决定下一 ...

  9. MFC-消息分派

    前言 由于工作需要,这几天学了一点MFC,在AFX里看到很多熟悉的东西,如类型信息,序列化,窗口封装和消息分派.几乎每个界面库都必须提供这些基础服务,但提供的手法却千差万别.MFC大量地借用了宏,映射 ...

  10. [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)

    循环队列 为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量.存储在其中的队列称为循环队列(Circular Queue). ...