android系统短信库的一些用法
生活随笔
收集整理的這篇文章主要介紹了
android系统短信库的一些用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、查詢(xún)所有短信,按發(fā)件人進(jìn)行分組
Cursor mCursor =managedQuery(Uri.parse("content://sms"),new String[] {"_id,address,date,read,status,type,body,count(address) as "+ "totleCount from (select _id,substr(address,4) as address,date,read,status,type,body "+ "from sms where address like \"+86%\" union select _id,address,date,read,status,type,body "+ "from sms where address not like \"+86%\") r group by r.address order by r.date desc --"},null,null,null);
?
?
2、刪除一個(gè)聯(lián)系人的所有短信會(huì)話,包括+86的號(hào)碼
/*** 刪除一個(gè)聯(lián)系人的所有短信會(huì)話,包括+86的號(hào)碼* @param phone*/public int deleteMsgSession(Context context, String phone){String phoneBytitle = "";if (!phone.startsWith("+86")){phoneBytitle = "+86" + phone;}else{phoneBytitle = phone.substring(3);}Cursor cursor =context.getContentResolver().query(Uri.parse("content://sms"), new String[] {"distinct thread_id"}, "address = ? or address = ?", new String[] {phone, phoneBytitle}, null);List<String> list = new ArrayList<String>();if (null != cursor){if (cursor.moveToFirst()){do{int thread_id = cursor.getInt(0);list.add(String.valueOf(thread_id));} while (cursor.moveToNext());}}if (null != cursor){cursor.close();cursor = null;} int size = list.size();if(size == 0){return -1;}else{int num = 0;for (int i = 0; i < size; i++){int res = context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + list.get(i)),null, null);num = num + res;}// System.out.println("sms_num:" + num);return num;}}
3、向系統(tǒng)庫(kù)插入短信、版本不同插入的字段有所區(qū)別
/*** 將發(fā)送的短信保存到系統(tǒng)短信庫(kù)中*/private void foreverSendMsg(String content){ContentValues values = new ContentValues();//系統(tǒng)SDK的版本號(hào)String sdkVersion = android.os.Build.VERSION.SDK;try{// 發(fā)送時(shí)間values.put("date", System.currentTimeMillis());// 閱讀狀態(tài)values.put("read", 1);// 送達(dá)號(hào)碼values.put("address", phoneNumberTextView.getText().toString());// 送達(dá)內(nèi)容values.put("body", content);//SDK為2.1時(shí),插入的字段if(ConstValue.SDK_VERSION == Integer.valueOf(sdkVersion)){values.put("status", -1);values.put("type", 2);// values.put("locked", 0);}else{// 設(shè)置可見(jiàn)values.put("seen", 1);}getContentResolver().insert(Uri.parse("content://sms/sent"), values);}catch (Exception e){e.printStackTrace();}finally{values = null;}
?/**
* @author 張興業(yè)
* 郵箱:xy-zhang#163.com
* android開(kāi)發(fā)進(jìn)階群:278401545
*
*/
轉(zhuǎn)載于:https://www.cnblogs.com/xyzlmn/archive/2011/12/15/3168137.html
總結(jié)
以上是生活随笔為你收集整理的android系统短信库的一些用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java源程序结构
- 下一篇: C#WinForm的线程及Invoke应