我希望得到一个地址列表和相应的Thread_id列表,所以我写了函数GetListPhoneNumberForConversation
和GetListThread_idForConversation
。
您知道有时地址会在草稿短信中重新生成null。 请查看文档getting "null" as an address for a draft sms
所以,我使用max(地址)作为参数的顺序,我希望它可以排除空地址记录。
它可以返回非空地址,但我不知道我的代码是否正确,地址列表和Thread_id列表是否匹配。
private static List<String> GetListPhoneNumberForConversation(Context myContext){
return GetListByField(myContext,"address");
}
public static List<String> GetListThread_idForConversation(Context myContext){
return GetListByField(myContext,"thread_id");
}
private static List<String> GetListByField(Context myContext,String field){
List<String> listbyField=new ArrayList<String>();
Uri uri=PublicParFun.GetUriBySMSRange(SMSRange.All);
String[] projection = new String[] {field};
Cursor cur = myContext.getContentResolver().query(uri,
projection,
"1=1 ) group by ( thread_id", null, "max(address)");
listbyField=FillListField(cur,field);
cur.close();
return listbyField;
}
private static List<String> FillListField(Cursor cur,String field){
List<String> mListField=new ArrayList<String>();
while(cur.moveToNext()){
String s=cur.getString(cur.getColumnIndex(field));
if (s!=null) {
mListField.add(s);
}
}
return mListField;
}
public static Uri GetUriBySMSRange(SMSRange mSMSRange){
Uri uri=null;
final String SMS_URI_ALL = "content://sms/";
final String SMS_URI_INBOX = "content://sms/inbox";
final String SMS_URI_SEND = "content://sms/sent";
final String SMS_URI_OUTBOX = "content://sms/outbox";
final String SMS_URI_DRAFT = "content://sms/draft";
switch (mSMSRange){
case All:
uri = Uri.parse(SMS_URI_ALL);
break;
case Inbox:
uri = Uri.parse(SMS_URI_INBOX);
break;
case Sentbox:
uri = Uri.parse(SMS_URI_SEND);
break;
case Outbox:
uri = Uri.parse(SMS_URI_OUTBOX);
break;
case Draft:
uri = Uri.parse(SMS_URI_DRAFT);
break;
}
return uri;
}