查询系统源码看短信的显示如何写:
在providers.telephony:SmsProvider:
1.确定怎么查数据库中的表:
Uri = content://sms/conversations
2.确定查询语句: select 映射条目 from 表 where 查询条件
表:
sms, (SELECT thread_id AS group_thread_id, MAX(date)AS group_date,COUNT(*) AS msg_count FROM sms GROUP BY thread_id) AS groups
查询条件:
sms.thread_id = groups.group_thread_id AND sms.date = groups.group_date
映射条目:
sms.body AS snippet
sms.thread_id AS thread_id
groups.msg_count AS msg_count
所以查询语句:
select
sms.body AS snippet,sms.thread_id AS thread_id,groups.msg_count AS msg_count
from
sms, (SELECT thread_id AS group_thread_id, MAX(date)AS group_date,COUNT(*) AS msg_count FROM sms GROUP BY thread_id) AS groups
where
sms.thread_id = groups.group_thread_id AND sms.date = groups.group_date
查询结果:
还需要号码,日期/时间
但看系统源码的映射列都已经写死,用户若添加自己需要的列会将系统的列覆盖:
看系统如何解决:找query:
这里的query是用户来调用的,所以projectionIn是用户传进来的,
继续找:同一个方法内,看query怎么处理projictionIn的,
点击查看如何处理用户传进来的projectionIn的,
再次点击发现进不去,为本类的方法:查找:
第一个在判断里面,略去不看,看第二个:将projection等各种参数建立成为一个sql语句,点击,发现进不去,,buildQuery为一个本类方法:查找其实现:
点击发现是一个本类方法,查找其实现:
分析源码:
将需要添加的列写好,测试正确性:
select sms.body AS snippet,sms.thread_id AS thread_id,groups.msg_count AS msg_count,sms.address as address,sms.date as date
from sms, (SELECT thread_id AS group_thread_id, MAX(date)AS group_date,COUNT(*) AS msg_count FROM sms GROUP BY thread_id) AS groups
where sms.thread_id = groups.group_thread_id AND sms.date = groups.group_date
详细代码:
1.
- package com.itcode.SMSManager.utils;
- import android.net.Uri;
- public class SmsUtils {
- public static final Uri SMSURL = Uri.parse("content://sms/conversations");
- }
2.
- package com.itcode.SMSManager;
- import android.app.Activity;
- import android.content.AsyncQueryHandler;
- import android.content.ContentResolver;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.util.Log;
- import com.itcode.SMSManager.utils.SmsUtils;
- public class ConversationActivity extends Activity {
- private ConversationAsyncQueryHandler asyncQuery;
- private static final String[] CONVERSATIONSMSPROJECTION = {
- "sms.body AS snippet", "sms.thread_id AS thread_id",
- "groups.msg_count AS msg_count", "sms.address as address",
- "sms.date as date" };
- public static final String TAG = "ConversationActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_conversion);
- asyncQuery = new ConversationAsyncQueryHandler(getContentResolver());
- asyncQuery.startQuery(0, null, SmsUtils.SMSURL,
- CONVERSATIONSMSPROJECTION, null, null, "date DESC");
- }
- private class ConversationAsyncQueryHandler extends AsyncQueryHandler {
- private String value;
- private String columnName;
- public ConversationAsyncQueryHandler(ContentResolver cr) {
- super(cr);
- // TODO Auto-generated constructor stub
- }
- /**
- * 当异步查询结束后调用的方法
- * Called when an asynchronous query is completed.
- * token the token to identify the query, passed in from startQuery.
- * cookie 添加额外的数据的 the cookie object passed in from startQuery.
- * cursor 查询出来的结果集The cursor holding the results from the query.
- */
- @Override
- protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
- // TODO Auto-generated method stub
- super.onQueryComplete(token, cookie, cursor);
- if(cursor != null && cursor.getCount()>0){
- int columnCount = cursor.getColumnCount();
- while(cursor.moveToNext()){
- for(int i=0;i<columnCount;i++){
- columnName = cursor.getColumnName(i);
- value = cursor.getString(i);
- Log.i(TAG,"当前是第"+cursor.getPosition() + "行:" + columnName + ":" + value);
- }
- }
- }
- }
- }
- }
运行结果: