1、查询所有短信,按发件人进行分组
- 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、删除一个联系人的所有短信会话,包括+86的号码
-
-
-
-
-
-
-
-
- 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、向系统库插入短信、版本不同插入的字段有所区别
-
-
-
-
-
-
- private void foreverSendMsg(String content)
-
- {
-
- ContentValues values = new ContentValues();
-
-
-
- String sdkVersion = android.os.Build.VERSION.SDK;
-
- try
-
- {
-
-
-
- values.put("date", System.currentTimeMillis());
-
-
-
- values.put("read", 1);
-
-
-
- values.put("address", phoneNumberTextView.getText().toString());
-
-
-
- values.put("body", content);
-
-
-
-
-
- if(ConstValue.SDK_VERSION == Integer.valueOf(sdkVersion))
-
- {
-
- values.put("status", -1);
-
- values.put("type", 2);
-
-
-
- }
-
- else
-
- {
-
-
-
- values.put("seen", 1);
-
- }
-
-
-
- getContentResolver().insert(Uri.parse("content://sms/sent"), values);
-
- }
-
- catch (Exception e)
-
- {
-
- e.printStackTrace();
-
- }
-
- finally
-
- {
-
- values = null;
-
- }
本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817121,如需转载请自行联系原作者