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中讀取的短信文件有
01 |
/** |
02 |
*
所有的短信 |
03 |
*/ |
04 |
public static final String
SMS_URI_ALL = "content://sms/" ; |
05 |
/** |
06 |
*
收件箱短信 |
07 |
*/ |
08 |
public static final String
SMS_URI_INBOX = "content://sms/inbox" ; |
09 |
/** |
10 |
*
已發送短信 |
11 |
*/ |
12 |
public static final String
SMS_URI_SEND = "content://sms/sent" ; |
13 |
/** |
14 |
*
草稿箱短信 |
15 |
*/ |
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來提取各種信息如下:
01 |
package com.pei.info; |
02 |
03 |
/** |
04 |
*
class name:SmsInfo<BR> |
05 |
*
class description:獲取短信各種信息的類<BR> |
06 |
*
PS: <BR> |
07 |
*
Date:2012-3-19<BR> |
08 |
* |
09 |
*
@version 1.00 |
10 |
*
@author CODYY)peijiangping |
11 |
*/ |
12 |
public class SmsInfo
{ |
13 |
/** |
14 |
*
短信內容 |
15 |
*/ |
16 |
private String
smsbody; |
17 |
/** |
18 |
*
發送短信的電話號碼 |
19 |
*/ |
20 |
private String
phoneNumber; |
21 |
/** |
22 |
*
發送短信的日期和時間 |
23 |
*/ |
24 |
private String
date; |
25 |
/** |
26 |
*
發送短信人的姓名 |
27 |
*/ |
28 |
private String
name; |
29 |
/** |
30 |
*
短信類型1是接收到的,2是已發出 |
31 |
*/ |
32 |
private String
type; |
33 |
34 |
public String
getSmsbody() { |
35 |
return smsbody; |
36 |
} |
37 |
38 |
public void setSmsbody(String
smsbody) { |
39 |
this .smsbody
= smsbody; |
40 |
} |
41 |
42 |
public String
getPhoneNumber() { |
43 |
return phoneNumber; |
44 |
} |
45 |
46 |
public void setPhoneNumber(String
phoneNumber) { |
47 |
this .phoneNumber
= phoneNumber; |
48 |
} |
49 |
50 |
public String
getDate() { |
51 |
return date; |
52 |
} |
53 |
54 |
public void setDate(String
date) { |
55 |
this .date
= date; |
56 |
} |
57 |
58 |
public String
getName() { |
59 |
return name; |
60 |
} |
61 |
62 |
public void setName(String
name) { |
63 |
this .name
= name; |
64 |
} |
65 |
66 |
public String
getType() { |
67 |
return type; |
68 |
} |
69 |
70 |
public void setType(String
type) { |
71 |
this .type
= type; |
72 |
} |
73 |
} |
然後就是封裝類,讀取信息內容SmsContent.java
01 |
package com.pei.util; |
02 |
03 |
import java.util.ArrayList; |
04 |
import java.util.List; |
05 |
06 |
import com.pei.info.SmsInfo; |
07 |
08 |
import android.app.Activity; |
09 |
import android.database.Cursor; |
10 |
import android.net.Uri; |
11 |
12 |
/** |
13 |
*
class name:SmsChoose<BR> |
14 |
*
class description:獲取手機中的各種短信信息<BR> |
15 |
*
PS: 需要權限 <uses-permission android:name="android.permission.READ_SMS" /><BR> |
16 |
*
Date:2012-3-19<BR> |
17 |
* |
18 |
*
@version 1.00 |
19 |
*
@author CODYY)peijiangping |
20 |
*/ |
21 |
public class SmsContent
{ |
22 |
private Activity
activity; //這裏有個activity對象,不知道爲啥以前好像不要,現在就要了。自己試試吧。 |
23 |
private Uri
uri; |
24 |
List<SmsInfo>
infos; |
25 |
26 |
public SmsContent(Activity
activity, Uri uri) { |
27 |
infos
= new ArrayList<SmsInfo>(); |
28 |
this .activity
= activity; |
29 |
this .uri
= uri; |
30 |
} |
31 |
32 |
/** |
33 |
*
Role:獲取短信的各種信息 <BR> |
34 |
*
Date:2012-3-19 <BR> |
35 |
* |
36 |
*
@author CODYY)peijiangping |
37 |
*/ |
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 , |
42 |
"date
desc" ); |
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" ); |
48 |
if (cusor
!= null )
{ |
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)); |
56 |
infos.add(smsinfo); |
57 |
} |
58 |
cusor.close(); |
59 |
} |
60 |
return infos; |
61 |
} |
62 |
} |
在提供一個listview來顯示短信內容:
01 |
package com.pei.activity; |
02 |
03 |
import java.util.List; |
04 |
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; |
18 |
19 |
/** |
20 |
*
class name:SmsListActivity<BR> |
21 |
*
class description:顯示短信的列表<BR> |
22 |
*
PS: <BR> |
23 |
*
Date:2012-3-19<BR> |
24 |
* |
25 |
*
@version 1.00 |
26 |
*
@author CODYY)peijiangping |
27 |
*/ |
28 |
public class SmsListActivity extends Activity
{ |
29 |
private ListView
listview; |
30 |
private List<SmsInfo>
infos; |
31 |
32 |
@Override |
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 )); |
41 |
} |
42 |
43 |
class SmsListAdapter extends BaseAdapter
{ |
44 |
private LayoutInflater
layoutinflater; |
45 |
private View
myview; |
46 |
47 |
public SmsListAdapter(Context
c) { |
48 |
layoutinflater
= LayoutInflater.from(c); |
49 |
} |
50 |
51 |
@Override |
52 |
public int getCount()
{ |
53 |
//
TODO Auto-generated method stub |
54 |
return infos.size(); |
55 |
} |
56 |
57 |
@Override |
58 |
public Object
getItem( int position)
{ |
59 |
//
TODO Auto-generated method stub |
60 |
return null ; |
61 |
} |
62 |
63 |
@Override |
64 |
public long getItemId( int position)
{ |
65 |
//
TODO Auto-generated method stub |
66 |
return 0 ; |
67 |
} |
68 |
69 |
@Override |
70 |
public View
getView( int position,
View convertView, ViewGroup parent) { |
71 |
if (convertView
== null )
{ |
72 |
myview
= layoutinflater.inflate(R.layout.smsitem, null ); |
73 |
} |
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()); |
80 |
return myview; |
81 |
} |
82 |
83 |
} |
84 |
} |
這樣你就可以隨意的讀取各種短信的各種內容了,顯示也十分方便。方法還可以繼續封裝。有待改進。