/**
* 读取系统短信方法
*
@param
view
*/
public
void
readSms(View view){
ContentResolver resolver=getContentResolver();
//创建ContentResolver对象,得到内容解析者
Uri uri= Uri. parse(
"content://sms/"
);
//全部短信的uri
Cursor cursor = resolver.query(uri,
null
,
null
,
null
,
null
);
//得到结果集
while
(cursor.moveToNext()){
String address=cursor.getString(cursor.getColumnIndex(
"address"
));
//得到地址
long
date=cursor.getLong(cursor.getColumnIndex(
"date"
));
//得到时间
String body=cursor.getString(cursor.getColumnIndex(
"body"
));
//内容
String type=cursor.getString(cursor.getColumnIndex(
"type"
));
//短信类型
SmsBean smsBean=
new
SmsBean();
smsBean.setAddress(address);
smsBean.setBody(body);
smsBean.setDate(date);
smsBean.setType(type);
tv
.setText(smsBean.toString());
/*if("1".equals(type)){
System.out.println("接收到的短信:");
}else{
System.out.println("发送的短信");
}
System.out.println("号码:"+address+" 时间:"+date+" 内容:"+body); //打印短信信息
*/
}
cursor.close();
}
---------------------------------------------------------------------------------------------------
public
void
testBatch()
throws
Exception {
ContentResolver resolver = getContext().getContentResolver();
ArrayList<ContentProviderOperation> list =
new
ArrayList<ContentProviderOperation>();
ContentProviderOperation operation1 = ContentProviderOperation.newInsert(
rawContactsUri
)
.withValue(
"_id"
,
null
)
.build();
ContentProviderOperation operation2 = ContentProviderOperation.newInsert(
dataUri
)
.withValueBackReference(
"raw_contact_id"
, 0)
// 把同组第一个操作的结果作为值
.withValue(
"mimetype"
,
"vnd.android.cursor.item/name"
)
.withValue(
"data1"
,
"CR"
)
.build();
ContentProviderOperation operation3 = ContentProviderOperation.newInsert(
dataUri
)
.withValueBackReference(
"raw_contact_id"
, 0)
.withValue(
"mimetype"
,
"vnd.android.cursor.item/phone_v2"
)
.withValue(
"data1"
,
"131999888888"
)
.build();
ContentProviderOperation operation4 = ContentProviderOperation.newInsert(
dataUri
)
.withValueBackReference(
"raw_contact_id"
, 0)
.withValue(
"mimetype"
,
"vnd.android.cursor.item/email_v2"
)
.withValue(
"data1"
,
"cr@hotmail.com"
)
.build();
list.add(operation1);
list.add( operation2);
list.add(operation3);
list.add(operation4);
resolver.applyBatch(
"com.android.contacts"
, list);
}
}