[sms] 03_短信详情界面

Android 4.0

短信详情界面
package cn.zengfansheng.ui;
import android.app.Activity;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.PhoneLookup;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.widget.ImageView;
import android.widget.TextView;
import cn.zengfansheng.intellectualsms.R;
import cn.zengfansheng.utils.Sms;
/**
 * 短信详情界面
 * 
 * @author hacket
 * 
 */
public class SmsDetailActivity extends Activity {
    private ImageView ivContactIcon;
    private TextView tvContactName;
    private TextView tvContactNumber;
    private TextView tvSmsType;
    private TextView tvSmsDate;
    private TextView tvSmsBody;
    private String _id;
    private static final String[] SMS_PROJECTION = new String[] { "_id",
            "address""type""date""body" };
    private static final int ID_COLUMN_INDEX = 0;
    private static final int ADDRESS_COLUMN_INDEX = 1;
    private static final int TYPE_COLUMN_INDEX = 2;
    private static final int DATE_COLUMN_INDEX = 3;
    private static final int BODY_COLUMN_INDEX = 4;
    private static final String[] CONTACT_PROJECTION = new String[] { PhoneLookup.DISPLAY_NAME };
    private static final int DISPLAY_NAME_COLUMN_INDEX = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_detail);
        intiView();
        getIntentData();
        startQuery();
    }
    /**
     * 3、异步查询数据
     */
    private void startQuery() {
        MyAsycnQueryHandler queryHandler = new MyAsycnQueryHandler(getContentResolver());
        Uri uri = Uri.parse("content://sms");
        String selection = "_id=?";
        String[] selectionArgs = {_id};
        queryHandler.startQuery(0, null, uri, SMS_PROJECTION, selection, selectionArgs, null);
    }
    /**
     * 3-2、异步查询实现类
     */
    private class MyAsycnQueryHandler extends AsyncQueryHandler {
        public MyAsycnQueryHandler(ContentResolver cr) {
            super(cr);
        }
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            super.onQueryComplete(token, cookie, cursor);
            setDate(cursor);
        }
    }
    
    /**
     * 3-3、设置数据
     */
    private void setDate(Cursor cursor) {
        if (!cursor.moveToNext()) {
            System.out.println("cursor没有数据");
            return;
        }
        System.out.println("cursor有数据");
        // 一、获取数据
        //1、联系人号码
        String contactNumber = cursor.getString(ADDRESS_COLUMN_INDEX);
        //2、联系人名称        
        String contactName = null;// 联系人姓名
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));
        Cursor contactsCursor = getContentResolver().query(uri , CONTACT_PROJECTION,nullnullnull);
        if (contactsCursor != null && contactsCursor.moveToNext()) {
            contactName = contactsCursor.getString(DISPLAY_NAME_COLUMN_INDEX);
        }
        if (contactsCursor != null) {
            contactsCursor.close();
        }
        //3、短信类型
        int smsType = cursor.getInt(TYPE_COLUMN_INDEX);
        String smsTypeStr = null;
        switch (smsType) {
        case Sms.INBOX.TYPE:
            smsTypeStr = "接收于";
            break;
        case Sms.SENT.TYPE:
            smsTypeStr = "发送于";
            break;
        }
        //4、短信日期
        Time time = new Time();
        time.setToNow();
        time.hour = 0;
        time.minute = 0;
        time.second = 0;
        long smsDate = cursor.getLong(DATE_COLUMN_INDEX);
        long firstsecondOfToday = time.toMillis(false);
        long dMillis = smsDate - firstsecondOfToday;
        String smsDateStr = null;
        if (dMillis > 0 && dMillis < DateUtils.DAY_IN_MILLIS) {// 86400000
            // 一天之内,显示时间
            smsDateStr = DateFormat.getTimeFormat(this).format(smsDate);
        } else {
            // 一天之外,显示日期
            smsDateStr = DateFormat.getDateFormat(this).format(smsDate);
        }
        //5、短信内容
        String smsBody = cursor.getString(BODY_COLUMN_INDEX);
        
        // 二、设置数据
        // 2-1、联系人数据
        if (contactName == null) {
            // 没有获取到联系人信息
            // ①联系人头像
            ivContactIcon.setImageDrawable(getResources()
                    .getDrawable(R.drawable.ic_unknown_picture_normal));
        } else {
            // 获取到了联系人信息
            // ①联系人头像
            ivContactIcon.setImageDrawable(getResources()
                    .getDrawable(R.drawable.ic_contact_picture));
            // ②联系人姓名
            tvContactName.setText(contactName);
        }
        // ②联系人号码
        tvContactNumber.setText(contactNumber);
        // 2-2、短信数据
        tvSmsType.setText(smsTypeStr);
        tvSmsDate.setText(smsDateStr);
        tvSmsBody.setText(smsBody);
    }
    /**
     * 2、获取传递过来的数据:短信_id
     */
    private void getIntentData() {
        Intent intent = getIntent();
        _id = intent.getStringExtra("_id");
    }
    /**
     * 1、初始化组件
     */
    private void intiView() {
        ivContactIcon = (ImageView) this.findViewById(R.id.iv_contact_icon);
        tvContactName = (TextView) this.findViewById(R.id.tv_contact_name);
        tvContactNumber = (TextView) this.findViewById(R.id.tv_contact_address);
        tvSmsType = (TextView) this.findViewById(R.id.tv_sms_type);
        tvSmsDate = (TextView) this.findViewById(R.id.tv_sms_date);
        tvSmsBody = (TextView) this.findViewById(R.id.tv_sms_body);
    }
}
异常:这是由于cursor没有向下移动的结果,cursor默认指向第一行的前面,也就是-1行
01-25 13:29:56.731: E/AndroidRuntime(1574): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
解决:将游标向下移动一行
cursor.moveToNext()