关闭

短信查询

458人阅读 评论(0) 收藏 举报

短信是记录在系统本地的mmssms.db表中的 /data/com.android.providers.telephony/databases中。

    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);

    这种是抛游标的方式,千万要小心处理,onPause()和onDestroy()要及时的关闭。

   if (null != mCursor && !mCursor.isClosed())
   {
       stopManagingCursor(mCursor);
   }

显示会话短信的Adapter可以使用ResourceCursorAdapter

public class FarmerAdapter extends ResourceCursorAdapter
{
    private Context mContext;
    public final class MessageHolder{
        public ImageView img_icon;
        public ImageView newSms;
        public TextView nameTextView;
        public TextView unreadCount;
        public int count;
        public TextView countTextView;
        public TextView draft;
        public TextView date;
        public TextView message;
        public String phoneNumber;
        public String name;
        public String id;}
    public FarmerAdapter(Context context, int layout, Cursor c)
    {
        super(context, layout, c);
        this.mContext = context;
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
       /**
        * 这边是写的一些逻辑代码
        */
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        View view = super.newView(context, cursor, parent);
        MessageHolder viewHolder = new MessageHolder();
        viewHolder.img_icon = (ImageView)view.findViewById(R.id.sms_headImage);
        viewHolder.nameTextView = (TextView)view.findViewById(R.id.name);
        viewHolder.unreadCount = (TextView)view.findViewById(R.id.unread_count);
        viewHolder.draft = (TextView)view.findViewById(R.id.draft);
        viewHolder.countTextView = (TextView)view.findViewById(R.id.all_count);
        viewHolder.date = (TextView)view.findViewById(R.id.date);
        viewHolder.message = (TextView)view.findViewById(R.id.sms_content);
        viewHolder.newSms = (ImageView)view.findViewById(R.id.newSms);
        view.setTag(viewHolder);
        return view;
    }
    @Override
    public void changeCursor(Cursor cursor)
    {
        super.changeCursor(cursor);
    }
}

可以自动的刷新界面,也可以手动刷新界面。当接收到短信时发送一个handler到短信显示界面 进行刷新。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场