2.AsyncQueryHandler、内容提供者
会话页面
Test :测试
public class Test extends AndroidTestCase{
public void test(){
Uri uri = Uri.parse("content://sms/conversations");
String[] projection={
"sms.body AS snippet",
"sms.thread_id AS thread_id",
"groups.msg_count AS msg_count",
"address as address",
"date as date"
};
Cursor cursor = getContext().getContentResolver().query(uri, projection, null, null, " date desc");
Tools.printCursor(cursor);
}
public void testNumber(){
String number = "8888";
Uri uri = PhoneLookup.CONTENT_FILTER_URI;
Uri uri2 = Uri.withAppendedPath(uri, number);
Cursor query = getContext().getContentResolver().query(uri2, null, null, null, null);
Tools.printCursor(query);
}
public void testNumber2(){
String number = "8888";
Uri uri = PhoneLookup.CONTENT_FILTER_URI;
Uri uri2 = Uri.withAppendedPath(uri, number);
Cursor query = getContext().getContentResolver().query(uri, null, null, null, null);
Tools.printCursor(query);
}
public void getFace(){
// 根据号码查询联系人的头像:
// Step1:查询联系人的id: URI为PhoneLookup.CONTENT_FILTER_URI
// Step2:查询ContactsContract.Contacts.CONTENT_URI + 加上上面得到id, 构建好Uri之后调用ContactsContract.Contacts.openContactPhotoInputStream得到图片的流.
String number ="2222";
int id = Tools.findIDByNumber(getContext(), number);
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, ""+id);
InputStream input = Contacts.openContactPhotoInputStream(getContext().getContentResolver(), uri);
System.out.println(input);
}
public void findContactFromsub(){
Uri uri = Phone.CONTENT_URI;
// Uri uri = PhoneLookup.CONTENT_FILTER_URI;
Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
Tools.printCursor(cursor);
}
}
Tools :
public class Tools {
public static void printCursor(Cursor cursor) {
if (cursor == null) {
System.out.println("cursor == null");
return;
}
if (cursor.getCount() == 0) {
System.out.println("cursor.getCount() == 0");
return;
}
while (cursor.moveToNext()) {
int columnCount = cursor.getColumnCount();
System.out.println("当前是第几行:" + cursor.getPosition());
for (int i = 0; i < columnCount; i++) {
String columnName = cursor.getColumnName(i);
String value = cursor.getString(i);
System.out.println(columnName + " : " + value);
}
}
}
/**
* 通过电话号码,查询联系人姓名
*
* @param ctx
* @param number
* 要查询的电话号码
* @return 返回联系人姓名,或者 null 表明联系人中,无此人
*/
public static String findNameByNumber(Context ctx, String number) {
String name = null;
Uri uri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number);
Cursor cursor = ctx.getContentResolver().query(uri2,
new String[] { "display_name" }, null, null, null);
if (cursor.getCount() == 0) {
return null;
} else {
// cursor 返回时,默认指向的是 第一行的上一行,即 -1 行 ,而所有的数据是从 第 0行开始的。
cursor.moveToNext();
name = cursor.getString(0);// cursor 仅查询一列内容,所以取的时候,列的索引值为 0
}
return name;
}
/**
* 通过电话号码,查询联系人ID
*
* @param ctx
* @param number
* 要查询的电话号码
* @return 返回联系人ID,或者 -1 表明无此联系人中
*/
public static int findIDByNumber(Context ctx, String number) {
int contactId;
Uri uri2 = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, number);
Cursor cursor = ctx.getContentResolver().query(uri2,
new String[] { "_id" }, null, null, null);
if (cursor.getCount() == 0) {
return -1;
} else {
// cursor 返回时,默认指向的是 第一行的上一行,即 -1 行 ,而所有的数据是从 第 0行开始的。
cursor.moveToNext();
contactId = cursor.getInt(0);// cursor 仅查询一列内容,所以取的时候,列的索引值为 0
}
return contactId;
}
/**
* 通过联系人的ID,查询联系人的头像
*
* @param ctx
* @param contactId
* @return 返回 bitmap 头像, 如果此联系人没有头像的话,返回 null
*/
public static Bitmap getFaceById(Context ctx, String contactId) {
Bitmap bitmap = null;
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_URI, contactId);
InputStream input = Contacts.openContactPhotoInputStream(ctx.getContentResolver(), uri);
if(input == null){
return null;
}
bitmap = BitmapFactory.decodeStream(input);
return bitmap;
}
/**
* 根据会话ID,删除短信
* @param ctx
* @param threadId
*/
public static void deleteMsgByThreadId(Context ctx, Integer threadId) {
ctx.getContentResolver().delete(MyConstants.URI_SMS, " thread_id = ?", new String[]{""+threadId});
}
}
MyQueryHandler
public class MyQueryHandler extends AsyncQueryHandler{
public MyQueryHandler(ContentResolver cr) {
super(cr);
}
@Override
/**
* 当startQuery 执行完成后,回调 此方法
* token 是startQuery 方法中的第一个参数
* cookie 是startQuery 方法中的第二个参数
* cursor 查询后的结果
*/
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
System.out.println("onQueryComplete : token:"+token);
System.out.println("onQueryComplete : cookie:"+cookie);
Tools.printCursor(cursor);
if(cookie!=null && cookie instanceof CursorAdapter){
CursorAdapter adapter = (CursorAdapter) cookie;
// 给adapter 设置新的cursor
adapter.changeCursor(cursor);
}
if(cursorChangedListener!=null){
cursorChangedListener.onCursorChanged(token, cookie, cursor);
}
}
public IOnCursorChangedListener getCursorChangedListener() {
return cursorChangedListener;
}
public void setOnCursorChangedListener(IOnCursorChangedListener cursorChangedListener) {
this.cursorChangedListener = cursorChangedListener;
}
private IOnCursorChangedListener cursorChangedListener;
/**
* 声明,cursor改变时的监听接口
* @author Administrator
*
*/
public interface IOnCursorChangedListener{
void onCursorChanged(int token, Object cookie, Cursor cursor);
}
}
MyConstants
public class MyConstants {
/**
* 查询会话的URI
*/
public static Uri URI_CONVERSATION = Uri.parse("content://sms/conversations");
/**
* 直接操作SMS表
*/
public static Uri URI_SMS = Uri.parse("content://sms");
/**
* 获得所有联系人的URI
*/
public static Uri URI_CONTACTS = Phone.CONTENT_URI;
/**
* 表示短信的类型 ,1 表示是接收到的短信
*/
public static int TYPE_RECEIVE = 1;
/**
* 表示短信的类型 ,2 表示是发送的短信
*/
public static int TYPE_SEND = 2;
}