转载时请注明转自:http://blog.csdn.net/sam_zhang1984
交流可加新浪微博:Android开发人
转自:http://griffinshi.javaeye.com/blog/685608
SMS管理
[功能]
1. 收信箱:显示所有收到的信息 且实时显示 即:当有新信息收到 能自动刷新显示
2. 发信箱:显示所有已发信息 同上
3. 编写新信息: 鉴于一些问题 打算不自行定义 而只通过Intent调用系统的
[原理]
1. 通过目标Uri显示收信箱 发信箱 目标Uri:content://sms/inbox content://sms/sent
2. 实时刷新:一个办法是开辟thread 定时查询目标Uri 显示之 但会带来一些效能影响 所以决定使用ContentObserve监听目标Uri 当有变动 由ContentObserve通知注册方 该Uri:content://sms
3. 注意:ContentObserve不能监听: content://sms/inbox & content://sms/sent 而只能监听content://sms
[代码 步骤]
1. 定义SMSObserver 用于监听目标 并通过Handle通知注册方
public class SMSObserver extends ContentObserver { public final static int SMS_CHANGE = 0; Handler handle; public SMSObserver(Handler h) { super(h); // TODO Auto-generated constructor stub handle = h; } public void onChange(boolean selfChange) { // TODO Auto-generated method stub super.onChange(selfChange); //notify SMSInbox & SMSSent handle.sendEmptyMessage(SMS_CHANGE); } }
2. 定义注册方:SMSInbox 鉴于SMSSent与其原理类似 故打算以SMSInbox为例
> 2.1. 显示当前所有收信箱 并与ListView适配
lv = (ListView)findViewById(R.id.list); cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null); adapter = new ItemAdapter(this); lv.setAdapter(adapter);
2.2. 定义Handle 用于接受变动 并注册与ContentObserve 当接到通知后 查询目标Uri 并刷新显示
handler = new Handler(){ public void handleMessage(Message msg){ if(msg.what == SMSObserver.SMS_CHANGE){ cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null); adapter.notifyDataSetChanged(); } } }; sObserver = new SMSObserver(handler); this.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, sObserver);
> 2.3. SMSInbox 仅用于显示 收信箱 故定义 SMSDetails extends Activity 用于详细显示 sms信息
- 2.3.1. 定义布局:details.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/detailsNumber" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/detailsBody" android:layout_width="fill_parent" android:layout_height="200dip" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="300dip" android:layout_height="wrap_content" > <Button android:id="@+id/detailsReply" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_gravity="left" android:text="回复" /> <Button android:id="@+id/detailsOK" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_gravity="right" android:text="确定" /> </LinearLayout> </LinearLayout>
2.3.2. 其中2个TextView 分别显示信息地址和正文 2个Button 一个用于关闭当前窗口 一个用于短信回复 且自动填充 收信人地址
public class SMSDetails extends Activity { TextView textNumber,textBody; Button btnOK,btnReply; OnClickListener cl; String address,body; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.details); textNumber = (TextView)findViewById(R.id.detailsNumber); textBody = (TextView)findViewById(R.id.detailsBody); btnReply = (Button)findViewById(R.id.detailsReply); btnOK = (Button)findViewById(R.id.detailsOK); Intent i = this.getIntent(); Bundle bundle = i.getExtras(); address = bundle.getString("address"); body = bundle.getString("body"); textNumber.setText("from:"+address); textBody.setText("message body:/n"+body); cl = new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch(arg0.getId()){ case R.id.detailsReply: sendGoNativeNew(address); break; case R.id.detailsOK: sendBack(); break; } } }; btnReply.setOnClickListener(cl); btnOK.setOnClickListener(cl); } public void sendGoNativeNew(String address){ //native send sms app Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms://")); //auto fill "address" sendIntent.putExtra("address", address); startActivity(sendIntent); } public void sendBack(){ Intent i = new Intent(); this.setResult(RESULT_OK, i); this.finish(); } }
2.3.3. 点击SMSInbox 某项 跳转到SMSDetails
1. lv.setOnItemClickListener(new OnItemClickListener(){ 2. @Override 3. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 4. long arg3) { 5. // TODO Auto-generated method stub 6. cursor.moveToPosition(arg2); 7. String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 8. String address = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString(); 9. 10. Bundle b = new Bundle(); 11. b.putString("body", body); 12. b.putString("address", address); 13. 14. Intent intent = new Intent(SMSInbox.this,SMSDetails.class); 15. intent.putExtras(b); 16. 17. startActivity(intent); 18. } 19. 20. });
- 2.3.4. 其中 item.xml 用于定义子项布局
1. <?xml version="1.0" encoding="utf-8"?> 2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3. android:orientation="vertical" 4. android:layout_width="fill_parent" 5. android:layout_height="fill_parent" 6. > 7. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 8. android:orientation="horizontal" 9. android:layout_width="wrap_content" 10. android:layout_height="wrap_content" 11. > 12. <ImageView 13. android:id="@+id/body" 14. android:layout_width="wrap_content" 15. android:layout_height="wrap_content" 16. /> 17. <TextView 18. android:id="@+id/num" 19. android:layout_width="wrap_content" 20. android:layout_height="wrap_content" 21. android:paddingLeft="5dip" 22. /> 23. </LinearLayout> 24. <TextView 25. android:id="@+id/body" 26. android:layout_width="fill_parent" 27. android:layout_height="wrap_content" 28. android:paddingLeft="10dip" 29. /> 30. </LinearLayout>
3. 鉴于SMSSent与SMSInbox大同小异 故不再细说 仅补上代码
1. public class SMSSent extends Activity { 2. ListView lv; 3. 4. Cursor cursor; 5. ItemAdapter adapter; 6. 7. SMSObserver sObserver; 8. Handler handler; 9. 10. /** Called when the activity is first created. */ 11. @Override 12. public void onCreate(Bundle savedInstanceState) { 13. super.onCreate(savedInstanceState); 14. setContentView(R.layout.list); 15. setTitle(R.string.sent); 16. 17. lv = (ListView)findViewById(R.id.list); 18. 19. cursor = getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, null); 20. 21. adapter = new ItemAdapter(this); 22. lv.setAdapter(adapter); 23. 24. lv.setOnItemClickListener(new OnItemClickListener(){ 25. @Override 26. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 27. long arg3) { 28. // TODO Auto-generated method stub 29. cursor.moveToPosition(arg2); 30. String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 31. String address = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString(); 32. 33. Bundle b = new Bundle(); 34. b.putString("body", body); 35. b.putString("address", address); 36. 37. Intent intent = new Intent(SMSSent.this,SMSDetails.class); 38. intent.putExtras(b); 39. 40. startActivity(intent); 41. } 42. 43. }); 44. 45. //register SMSObserve 46. handler = new Handler(){ 47. public void handleMessage(Message msg){ 48. if(msg.what == SMSObserver.SMS_CHANGE){ 49. cursor = getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, null); 50. adapter.notifyDataSetChanged(); 51. } 52. 53. } 54. }; 55. sObserver = new SMSObserver(handler); 56. this.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, sObserver); 57. 58. } 59. 60. 61. public class ItemAdapter extends BaseAdapter { 62. Activity activity; 63. 64. public ItemAdapter(Activity a){ 65. activity = a; 66. } 67. 68. @Override 69. public int getCount() { 70. // TODO Auto-generated method stub 71. return cursor.getCount(); 72. } 73. 74. @Override 75. public Object getItem(int arg0) { 76. // TODO Auto-generated method stub 77. return cursor.getString(arg0); 78. } 79. 80. @Override 81. public long getItemId(int arg0) { 82. // TODO Auto-generated method stub 83. return arg0; 84. } 85. 86. @Override 87. public View getView(int arg0, View arg1, ViewGroup arg2) { 88. // TODO Auto-generated method stub 89. return composeItem(arg0); 90. } 91. 92. private View composeItem(int position){ 93. cursor.moveToPosition(position); 94. String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString(); 95. String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString(); 96. 97. LinearLayout item = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.item, null); 98. 99. LinearLayout l1 = (LinearLayout)item.getChildAt(0); 100. 101. TextView tbody = (TextView)item.getChildAt(1); 102. if(tbody != null){ 103. tbody.setText(body); 104. } 105. 106. TextView tnum = (TextView)l1.getChildAt(1); 107. if(tnum != null){ 108. tnum.setText(number); 109. } 110. 111. ImageView image = (ImageView)l1.getChildAt(0); 112. image.setImageResource(R.drawable.message); 113. 114. return item; 115. } 116. 117. } 118. }
4. emulator 运行截图
> 4.1. SMSInbox:
- 4.1.1. 通过telnet localhost 5554 登录emulator 通过 sms send 123 hello to 123 模拟发送短信
- 4.1.2. 短信发送记录为:
1. Android Console: type 'help' for a list of commands 2. OK 3. sms send 12 ds 4. OK 5. sms send 23 hi to 23 6. OK 7. sms send 34 i am griddinshi 8. OK
4.1.3. SMSInbox:
5. 未解决问题:
> 5.1. ContentObserver 只能监听content://sms 而不支持content://sms/inbox content://sms/sent 个人猜测是因为:android 在写sms数据库 insert(...) 没有通过ContentResolver通知content://sms/inbox content://sms/sent 所致 即:没有以下代码:
getContext().getContentResolver().notifyChange(noteUri, null);
6. 如果写的有问题的 欢迎讨论 否则 请回帖支持一些