原理是通过,contentprovider获取系统短信数据库中的字段信息而达到获取内容目的
效果图如下:
具体代码如下:
[html]
view plain
copy
1. package com.internal.message;
2.
3. import java.text.SimpleDateFormat;
4. import java.util.ArrayList;
5. import java.util.Date;
6. import java.util.HashMap;
7. import java.util.List;
8. import java.util.Map;
9.
10. import android.app.ListActivity;
11. import android.content.ContentResolver;
12. import android.database.Cursor;
13. import android.database.sqlite.SQLiteException;
14. import android.net.Uri;
15. import android.os.Bundle;
16. import android.provider.ContactsContract;
17. import android.provider.ContactsContract.CommonDataKinds.Phone;
18. import android.provider.ContactsContract.PhoneLookup;
19. import android.util.Log;
20. import android.widget.ListView;
21. import android.widget.SimpleAdapter;
22.
23. public class QureSms extends ListActivity {
24.
25. smslist=null; //显示列表信息
26. <Map<String,Object>> mData= new ArrayList<Map<String,Object>>();
27. <String> title=new ArrayList<String>(); //短信来源
28. <String> text=new ArrayList<String>(); //短信内容
29.
30. @Override
31. protected void onCreate(Bundle savedInstanceState) {
32. super.onCreate(savedInstanceState);
33. smslist=getListView();
34. getSmsInPhone();
35. lengh = title.size();
36. i =0; i < lengh; i++) {
37. <String,Object> item = new HashMap<String,Object>();
38. item.put("title", title.get(i));
39. item.put("text", text.get(i));
40. mData.add(item);
41. }
42. adapter = new
43. new String[]{"title","text"},new int[]{android.R.id.text1,android.R.id.text2});
44. setListAdapter(adapter);
45.
46. }
47. /**
48. * 获取手机内所以短消息
49. */
50. private void getSmsInPhone(){
51. SMS_URI_ALL = "content://sms/";
52. SMS_URI_INBOX = "content://sms/inbox";
53. SMS_URI_SEND = "content://sms/sent";
54. SMS_URI_DRAFT = "content://sms/draft"; */
55.
56. try{
57. cr = getContentResolver();
58. projection = new
59. "body", "date", "type"};
60. uri = Uri.parse(SMS_URI_ALL);
61. cur = cr.query(uri, projection, null, null, "date desc");
62.
63. if (cur.moveToFirst()) {
64. String name;
65. String phoneNumber;
66. String smsbody;
67. String date;
68. String type;
69.
70. nameColumn = cur.getColumnIndex("person");
71. phoneNumberColumn = cur.getColumnIndex("address");
72. smsbodyColumn = cur.getColumnIndex("body");
73. dateColumn = cur.getColumnIndex("date");
74. typeColumn = cur.getColumnIndex("type");
75.
76. do{
77. phoneNumber = cur.getString(phoneNumberColumn);
78. name = cur.getString(nameColumn); 这样获取的联系认为空,所以我改用下面的方法获取
79. name=getPeopleNameFromPerson(phoneNumber);
80. smsbody = cur.getString(smsbodyColumn);
81.
82. dateFormat = new
83. "yyyy-MM-dd hh:mm:ss");
84. d = new
85. date = dateFormat.format(d);
86.
87. typeId = cur.getInt(typeColumn);
88. typeId
89. type = "接收";
90. typeId
91. type = "发送";
92. } else {
93. type = "草稿";
94. }
95.
96. title.add(type+" "+date+'\n'+phoneNumber);
97. text.add(name+'\n'+smsbody);
98.
99. smsbody == null) smsbody = "";
100. } while(cur.moveToNext());
101. }
102. cur.close();
103. cur=null;
104. } catch(SQLiteException ex) {
105. Log.e("SQLiteException in getSmsInPhone", ex.getMessage());
106. }
107.
108. }
109. /**
110. * 通过address手机号关联Contacts联系人的显示名字
111. * @param address
112. * @return
113. */
114. private String getPeopleNameFromPerson(String address){
115. address == null || address
116. return null;
117. }
118.
119. strPerson = "null";
120. projection = new
121.
122. uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address); // address 手机号过滤
123. cursor = getContentResolver().query(uri_Person, projection, null, null, null);
124.
125. if(cursor.moveToFirst()){
126. index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);
127. strPeopleName = cursor.getString(index_PeopleName);
128. strPerson = strPeopleName;
129. }
130. else{
131. strPerson = address;
132. }
133. cursor.close();
134. cursor=null;
135. return strPerson;
136. }
137.
138.
139. }