androidの读取短信内容
1. 读取短信内容,很简单,直接上代码
<span style="font-size:14px;"> public Uri SMS_INBOX = Uri.parse("content://sms/inbox");
private void fillListView() {
ListView view = (ListView) findViewById(R.id.sms_list);
ArrayList<HashMap<String, String>> list = readAllSMS();
SimpleAdapter listItemAdapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2,
new String[] {"addr","body"},
new int[] {android.R.id.text1, android.R.id.text2}
);
view.setAdapter(listItemAdapter);
}
private ArrayList<HashMap<String, String>> readAllSMS() {
Cursor cursor = managedQuery(SMS_INBOX, new String[] { "address", "person", "body" },
null, null, null );
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>> ();
if(cursor.moveToFirst()) {
int addrIdx = cursor.getColumnIndex("address");
int personIdx = cursor.getColumnIndex("person");
int bodyIdx = cursor.getColumnIndex("body");
do {
String addr = cursor.getString(addrIdx);
String person = cursor.getString(personIdx);
String body = cursor.getString(bodyIdx);
HashMap<String, String> item = new HashMap<String, String>();
item.put("addr", addr);
item.put("person", person);
item.put("body", body);
list.add(item);
} while(cursor.moveToNext());
}
return list;
}</span><span style="font-size:24px;">
</span>
这里读取短信中收件箱的短信。如果全部短信内容时候,更改为:
Uri SMS_INBOX = Uri.parse("content://sms");
Activity::managedQuery函数基本上是一个接收SQL查询的接口,参数分别为:
Parameters
uri The URI of the content provider to query.
projection List of columns to return.
selection SQL WHERE clause.
selectionArgs The arguments to selection, if any ?s are pesent
sortOrder SQL ORDER BY clause.
uri The URI of the content provider to query.
projection List of columns to return.
selection SQL WHERE clause.
selectionArgs The arguments to selection, if any ?s are pesent
sortOrder SQL ORDER BY clause.
要读取短信,需要加入权限:
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
关于content://sms/inbox表,大致包含的域有:
_id | 短消息序号 如100
thread_id | 对话的序号 如100
address | 发件人地址,手机号.如+8613811810000
person | 发件人,返回一个数字就是联系人列表里的序号,陌生人为null
date | 日期 long型。如1256539465022
protocol | 协议 0 SMS_RPOTO, 1 MMS_PROTO
read | 是否阅读 0未读, 1已读
status | 状态 -1接收,0 complete, 64 pending, 128 failed
type | 类型 1是接收到的,2是已发出
body | 短消息内容
service_center | 短信服务中心号码编号。如+8613800755500
关于短信相关的应用,大致还包括:监听短信数据库(可用于监听短信的到来)、发送短信。使用起来基本上还算简单。
_id | 短消息序号 如100
thread_id | 对话的序号 如100
address | 发件人地址,手机号.如+8613811810000
person | 发件人,返回一个数字就是联系人列表里的序号,陌生人为null
date | 日期 long型。如1256539465022
protocol | 协议 0 SMS_RPOTO, 1 MMS_PROTO
read | 是否阅读 0未读, 1已读
status | 状态 -1接收,0 complete, 64 pending, 128 failed
type | 类型 1是接收到的,2是已发出
body | 短消息内容
service_center | 短信服务中心号码编号。如+8613800755500
关于短信相关的应用,大致还包括:监听短信数据库(可用于监听短信的到来)、发送短信。使用起来基本上还算简单。