android会把短信信息保存在数据库中,可查看/dbdata/databases/com.android.providers.telephony/mmssms.db。但是我们不能直接访问数据库,只能通过ContentProvider来访问它。
以下是访问短信数据库的uri
content://sms/ 所有短信
content://sms/inbox 收件箱
content://sms/sent 已发送
content://sms/draft 草稿
content://sms/outbox 发件箱
content://sms/failed 发送失败
content://sms/queued 待发送列表
Android中读取的短信文件有
04 |
public static final String SMS_URI_ALL = "content://sms/" ; |
08 |
public static final String SMS_URI_INBOX = "content://sms/inbox" ; |
12 |
public static final String SMS_URI_SEND = "content://sms/sent" ; |
16 |
public static final String SMS_URI_DRAFT = "content://sms/draft" ; |
读取的短信信息有:
* _id:短信序号,如100
* thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的
* address:发件人地址,即手机号,如+8613811810000
* person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null
* date:日期,long型,如1256539465022,可以对日期显示格式进行设置
* protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信
* read:是否阅读0未读,1已读
* status:短信状态-1接收,0complete,64pending,128failed
* type:短信类型1是接收到的,2是已发出
* body:短信具体内容
* service_center:短信服务中心号码编号,如+8613800755500
下面我们可以先建立一个SmsInfo来提取各种信息如下:
04 |
* class name:SmsInfo<BR> |
05 |
* class description:获取短信各种信息的类<BR> |
10 |
* @author CODYY)peijiangping |
12 |
public class SmsInfo { |
16 |
private String smsbody; |
20 |
private String phoneNumber; |
34 |
public String getSmsbody() { |
38 |
public void setSmsbody(String smsbody) { |
39 |
this .smsbody = smsbody; |
42 |
public String getPhoneNumber() { |
46 |
public void setPhoneNumber(String phoneNumber) { |
47 |
this .phoneNumber = phoneNumber; |
50 |
public String getDate() { |
54 |
public void setDate(String date) { |
58 |
public String getName() { |
62 |
public void setName(String name) { |
66 |
public String getType() { |
70 |
public void setType(String type) { |
然后就是封装类,读取信息内容SmsContent.java
03 |
import java.util.ArrayList; |
04 |
import java.util.List; |
06 |
import com.pei.info.SmsInfo; |
08 |
import android.app.Activity; |
09 |
import android.database.Cursor; |
10 |
import android.net.Uri; |
13 |
* class name:SmsChoose<BR> |
14 |
* class description:获取手机中的各种短信信息<BR> |
15 |
* PS: 需要权限 <uses-permission android:name="android.permission.READ_SMS" /><BR> |
19 |
* @author CODYY)peijiangping |
21 |
public class SmsContent { |
22 |
private Activity activity; |
26 |
public SmsContent(Activity activity, Uri uri) { |
27 |
infos = new ArrayList<SmsInfo>(); |
28 |
this .activity = activity; |
36 |
* @author CODYY)peijiangping |
38 |
public List<SmsInfo> getSmsInfo() { |
39 |
String[] projection = new String[] { "_id" , "address" , "person" , |
40 |
"body" , "date" , "type" }; |
41 |
Cursor cusor = activity.managedQuery(uri, projection, null , null , |
43 |
int nameColumn = cusor.getColumnIndex( "person" ); |
44 |
int phoneNumberColumn = cusor.getColumnIndex( "address" ); |
45 |
int smsbodyColumn = cusor.getColumnIndex( "body" ); |
46 |
int dateColumn = cusor.getColumnIndex( "date" ); |
47 |
int typeColumn = cusor.getColumnIndex( "type" ); |
49 |
while (cusor.moveToNext()) { |
50 |
SmsInfo smsinfo = new SmsInfo(); |
51 |
smsinfo.setName(cusor.getString(nameColumn)); |
52 |
smsinfo.setDate(cusor.getString(dateColumn)); |
53 |
smsinfo.setPhoneNumber(cusor.getString(phoneNumberColumn)); |
54 |
smsinfo.setSmsbody(cusor.getString(smsbodyColumn)); |
55 |
smsinfo.setType(cusor.getString(typeColumn)); |
在提供一个listview来显示短信内容:
01 |
package com.pei.activity; |
03 |
import java.util.List; |
05 |
import com.pei.fixed.AllFinalInfo; |
06 |
import com.pei.info.SmsInfo; |
07 |
import com.pei.util.SmsContent; |
08 |
import android.app.Activity; |
09 |
import android.content.Context; |
10 |
import android.net.Uri; |
11 |
import android.os.Bundle; |
12 |
import android.view.LayoutInflater; |
13 |
import android.view.View; |
14 |
import android.view.ViewGroup; |
15 |
import android.widget.BaseAdapter; |
16 |
import android.widget.ListView; |
17 |
import android.widget.TextView; |
20 |
* class name:SmsListActivity<BR> |
21 |
* class description:显示短信的列表<BR> |
26 |
* @author CODYY)peijiangping |
28 |
public class SmsListActivity extends Activity { |
29 |
private ListView listview; |
30 |
private List<SmsInfo> infos; |
33 |
protected void onCreate(Bundle savedInstanceState) { |
34 |
super .onCreate(savedInstanceState); |
35 |
setContentView(R.layout.sms); |
36 |
Uri uri = Uri.parse(AllFinalInfo.SMS_URI_INBOX); |
37 |
SmsContent sc = new SmsContent( this , uri); |
38 |
infos = sc.getSmsInfo(); |
39 |
listview = (ListView) this .findViewById(R.id.ListView_Sms); |
40 |
listview.setAdapter( new SmsListAdapter( this )); |
43 |
class SmsListAdapter extends BaseAdapter { |
44 |
private LayoutInflater layoutinflater; |
47 |
public SmsListAdapter(Context c) { |
48 |
layoutinflater = LayoutInflater.from(c); |
52 |
public int getCount() { |
58 |
public Object getItem( int position) { |
64 |
public long getItemId( int position) { |
70 |
public View getView( int position, View convertView, ViewGroup parent) { |
71 |
if (convertView == null ) { |
72 |
myview = layoutinflater.inflate(R.layout.smsitem, null ); |
74 |
TextView body = (TextView) myview |
75 |
.findViewById(R.id.TextView_SmsBody); |
76 |
TextView name = (TextView) myview |
77 |
.findViewById(R.id.TextView_SmsName); |
78 |
body.setText(infos.get(position).getSmsbody()); |
79 |
name.setText(infos.get(position).getName()); |
这样你就可以随意的读取各种短信的各种内容了,显示也十分方便。方法还可以继续封装。有待改进。