短信查询

来源:互联网 发布:mac圣诞限量套装2017 编辑:程序博客网 时间:2024/04/29 20:57

短信是记录在系统本地的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到短信显示界面 进行刷新。

原创粉丝点击