adapter.java
package smalt.manger.sms; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import smalt.manager.main.SApplication; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.util.Log; /** * 獲取短信內容 * * @author Administrator * */ public class SmsUtil { public static final Uri MMSSMS_FULL_CONVERSATION_URI = Uri .parse("content://mms-sms/conversations"); public static final Uri CONVERSATION_URI = MMSSMS_FULL_CONVERSATION_URI .buildUpon().appendQueryParameter("simple", "true").build(); // 上下文,方便调用 static public Context mContext; /** * 得到分组的短信 里面应该有 联系人名称,短信对话的数量 最近一条短信的内容,最好还能有相关时间 最新一条短信是否已经阅读 * * @return */ static public List<SmsDataBean> getThreadSmsData() { mContext = SApplication.nowSApplication.getApplicationContext();// 得到上下文對象 Cursor smsThreadCursor = mContext.getContentResolver().query( MMSSMS_FULL_CONVERSATION_URI, new String[] { "body", "type", "date", "read", "thread_id", "address" }, null, null, "date desc"); // 存入所有會話 LinkedHashMap<String, SmsDataBean> smsDataMap = new LinkedHashMap<String, SmsDataBean>(); // 子線程數量 int threadCount = smsThreadCursor.getCount(); // 遍歷所有短信 for (int i = 0; i < threadCount; i++) { // 獲取短信 SmsDataBean nowSmsDataBean = new SmsDataBean(); // 向下移動一條記錄 smsThreadCursor.moveToNext(); nowSmsDataBean.smsBody = smsThreadCursor.getString(smsThreadCursor .getColumnIndex("body")); // 內容 nowSmsDataBean.smsBody = smsThreadCursor.getString(smsThreadCursor .getColumnIndex("address")); // 電話號碼 nowSmsDataBean.smsBody = smsThreadCursor.getString(smsThreadCursor .getColumnIndex("thread_id")); // 線程號 // 設置最新一條短信的時間 nowSmsDataBean.smsDate = smsThreadCursor.getLong(smsThreadCursor .getColumnIndex("date")); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); nowSmsDataBean.smsStrDate = sdf.format(new Date( nowSmsDataBean.smsDate)); // 查找有多少條未讀短信 { Uri queryNoReadThreadSmsCountUri = Uri.parse("content://sms/"); Cursor noReadThreadSmsCountCursor = mContext .getContentResolver().query( queryNoReadThreadSmsCountUri, new String[] { "read", "thread_id" }, "read = 0 and thread_id =" + nowSmsDataBean.smsThreadId, null, null); nowSmsDataBean.smsNoReadCount = noReadThreadSmsCountCursor .getCount(); noReadThreadSmsCountCursor.close(); } // 把以上數據加入smsDataList smsDataMap.put(nowSmsDataBean.smsThreadId, nowSmsDataBean); }// for-end smsThreadCursor.close();// 關閉遊標 Cursor simpleThreadDataCursor = mContext.getContentResolver().query( CONVERSATION_URI, new String[] { "_id", "message_count" }, null, null, "date desc"); // 循環匹配所有短信會話,設置會話短信數量 int simpleThreadCount = simpleThreadDataCursor.getCount(); // 循環將會話數量寫入map for (int i = 0; i < simpleThreadCount; i++) { simpleThreadDataCursor.moveToNext(); String simpleId = simpleThreadDataCursor .getString(simpleThreadDataCursor.getColumnIndex("_id")); SmsDataBean nowSmsDataBean = smsDataMap.get(simpleId); if (nowSmsDataBean != null) { nowSmsDataBean.smsAllCount = simpleThreadDataCursor .getString(simpleThreadDataCursor .getColumnIndex("message_count")); } }// for-end simpleThreadDataCursor.close(); List<SmsDataBean> returnSmsListData = new ArrayList<SmsDataBean>( smsDataMap.values()); return returnSmsListData; } /** * 取得聯繫人名字 * * @param smsThreadCursor */ static void testColNameAndDatas(Cursor smsThreadCursor) { // 短信會話數量 int crusorCount = smsThreadCursor.getCount(); // 短信會話相關數據列 String[] allColName = smsThreadCursor.getColumnNames(); for (int i = 0; i < crusorCount; i++) { smsThreadCursor.moveToNext(); for (int j = 0; j < allColName.length; j++) { Log.i("smsdata", "列名:" + allColName[j] + "數據:" + smsThreadCursor.getString(j)); } Log.i("smsdata", "`````````````````````"); } }// testColNameAndDatas-end /** * 还应该有一个方法 // 能根据指定的ThreadID 取得所有的相关 // 发出和收到的短信 * * @param queryThreadId * @return */ static public List<SmsDataBean> getSmsDataListByThreadId( String queryThreadId) { // 得到上下文 mContext = SApplication.nowSApplication.getApplicationContext(); // 查找所有的短信 Uri smsUri = Uri.parse("content://sms/"); // 按照threadId进行过滤 Cursor smsDataCursor = mContext.getContentResolver().query(smsUri, null, "thread_id = " + queryThreadId, null, "date desc"); testColNameAndDatas(smsDataCursor);// 測試是否拿到數據 smsDataCursor.close(); return null; } }
thread_id字段出現在以下表中:
sms表:存儲短信字段
thread_id是integer類型,表示threads表的id,表示往canonical_addresses表中插入信息
threads表是一組回話,例如號碼為139777777和號碼為135888888的所有短信稱為一組會話,thread_id从1开始,按最早的时间开始排序,最早时间thread_id为1
pdu表:pdu为承载着一条短信的所有短信。
一条短信为140个英文字符长度,在这个长度范围内,即需一个pdu即可。超出这个范围,即要分割成多个pdu数组。
thread_id是integer類型,表示threads表的id
MmsSmsProvider中的mms-sms中有threadID字段,屬於URI_THREAD_ID類型,用於获取指定接收者(附加的查询参数recipient)的会话id,若不存在,新增
activity.java
package smalt.manager.main.demo; import java.util.List; import smalt.manager.main.R; import smalt.manger.sms.SmsAdapter; import smalt.manger.sms.SmsDataBean; import smalt.manger.sms.SmsUtil; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; /** * 测试类,在这里测试好以后再copy到mainActivity * * @author Administrator * */ public class DemoDay01Activity extends Activity { ListView threadListView; List<SmsDataBean> smsThreadListData; SmsAdapter mSmsThreadAdapter; void initView() { } void initData() { } void initListener() { } public void onCreate(Bundle savedInstanceState) { // 测试短信会话的数据 super.onCreate(savedInstanceState); setContentView(R.layout.sms_main); SmsUtil.getSmsDataListByThreadId("3"); initView(); initData(); initListener(); } public void onResume() { super.onResume(); } }