监听短信数据库变化

void android.content.ContentResolver.registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer)

Register an observer class that gets callbacks when data identified by a given content URI changes.

Parameters:

uri The URI to watch for changes. This can be a specific row URI, or a base URI for a whole class of content.

notifyForDescendents If true changes to URIs beginning with uri will also cause notifications to be sent. If false only changes to the exact URI specified by uri will cause notifications to be sent.

If true, than any URI values at or below the specified URI will also trigger a match.

observer The object that receives callbacks when changes occur.

See Also: unregisterContentObserver

    final int WAHT_ONCHANGE = 0;
    
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.i(tag, "++handleMessage++");
            switch(msg.what)
            {
            case WAHT_ONCHANGE:
                Toast.makeText(SendMsgActivity.this, "有新的消息", Toast.LENGTH_SHORT).show();
                break;
            }
        }

    };
    
    ContentObserver observer = new ContentObserver(handler) {
        
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            watchNewInboxSMS();
            handler.sendEmptyMessage(WAHT_ONCHANGE);
        }
    };
    
    void registerObserver()
    {
        Log.i(tag, "++registerObserver++");
        Uri uri = Uri.parse("content://sms/");//inbox");
        getContentResolver().registerContentObserver(uri, true, observer);
    }
    
    // 查看收件箱中未读的信息
    public void watchNewInboxSMS() {
        Log.i(tag, "++watchNewInboxSMS++");
        Uri uri = Uri.parse("content://sms/inbox");
        String[] projection = { "_id", "address", "person",
                "body", "date", "type", "read"};
        String selection = "read = ?";
        String[] selectionArgs = {"0"};
        String sortOrder = "date desc";
        Cursor cursor = getContentResolver().query(uri, projection, selection,
                selectionArgs, sortOrder);
        if (!cursor.moveToFirst()) {
            return;
        }
        int i = 0;
        do {
            String name = cursor.getString(cursor.getColumnIndex("person"));
            String number = cursor.getString(cursor.getColumnIndex("address"));
            String body = cursor.getString(cursor.getColumnIndex("body"));
            String date = fmtDate(cursor.getString(cursor
                    .getColumnIndex("date")));
            String type = fmtType(cursor.getInt(cursor.getColumnIndex("type")));
            Log.i(tag, "++第" + (++i) + "条未读短信++\nname:" + name + ",\nnumber:"
                    + number + ",\nbody:" + body + ",\ndate:" + date
                    + ",\ntype:" + type);
        } while (cursor.moveToNext());
    }

当手机收到新信息时,从短信数据库中取得未读信息,在日志打印出来,并显示一条toast信息;当某条信息被删掉时,也会被监听到,显示一条toast信息。

android 使用contentobserver监听数据库内容变化

06-20

转自:[url=http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=37427&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D67]http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=37427&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D67[/url]rn在android中经常会用到改变数据库内容后再去使用数据库更新的内容,很多人会重新去query一遍,但是这样的问题就是程序会特别占内存,而且有可能会搂关cursor而导致程序内存未释放等等。其实android内部提供了一种ContentObserver的东西来监听数据库内容的变化。rnContentObserver的构造函数需要一个参数Hanlder,因为ContentObserver内部使用了一个实现Runnable接口的内部类NotificationRunnable,来实现数据库内容的变化。需要使用hanlder去post消息。注册ContentObserver的方法是:getContentResolver().registerContentObserver(uri, notifyForDescendents, observer).rn上面3个参数为:uri----Uri类型,是需要监听的数据库的uri.rn notifyForDescendents---boolean true的话就会监听所有与此uri相关的uri。false的话则是直接特殊的uri才会监听。一般都设置为true.rn observer-----ContentObserver 就是需要的contentobserver.rn初始化一个ContentObserver对象,重载onChange(boolean ),在这个方法里去操作数据库的使用,针对变化后的使用。rn写了一个小demo,可以参考下。提示这种监听方式必须是contentprovider才能使用,因为contentprovider有uri.简单的那种sqlite数据库没有uri是使用不了的。rn下面demo操作的是在一个activityA里点击button跳转至另外一个activityB,在B中点击button往数据库中加数据,加完后关闭B回到A。A的button的文字自动变化设置到数据库中的字符串。rnrn[url=http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=37427&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D67]请在此查看代码[/url] 论坛

没有更多推荐了,返回首页