Android ContentObserver:如何只获取新的更新数据?

时间:2013-07-13 12:14:02

标签: android contentobserver

我在我的应用中注册了SMS ContentObserver。我想用MessageID更新我的ArrayList onChange。这是我的ObServer代码..

public class SmsObserver extends ContentObserver {

    private Context mContext;

    private String contactId = "", contactName = "";
    private String smsBodyStr = "", phoneNoStr = "";
    private long smsDatTime = System.currentTimeMillis();

    static final Uri SMS_STATUS_URI = Uri.parse("content://sms");

    public SmsObserver(Handler handler, Context ctx) {
        super(handler);
        mContext = ctx;
    }     

    public boolean deliverSelfNotifications() {
        return true;
    }

    public void onChange(boolean selfChange) {
        try{
            Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null);
            if (sms_sent_cursor != null) {
                if (sms_sent_cursor.moveToFirst()) {
                    String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
                    if(protocol == null){
                        int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex        ("type"));                       
                        if(type == 2){
                            smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim();
                            phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim();
                            smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"));
                        }
                    }
                }
            } else {
                Log.e("Info","Send Cursor is Empty");
            }
        } catch(Exception sggh) {
            Log.e("Error", "Error on onChange : "+sggh.toString());
        }
        super.onChange(selfChange);
    }
}

但是,我发送了两条消息。第一个是电话关闭。第二是电话打开。第一个更新是后者。第二次更新是我发送手机的第一次更新。有了这个ObServer,我只能访问最新的帖子。仅显示最新数据。如何获取新的更新数据?

0 个答案:

没有答案