zoukankan      html  css  js  c++  java
  • 监听短信数据库变化

    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信息。

  • 相关阅读:
    [03] html 中引入与使用css
    [04] 前端构建工具区别
    [04]测试框架杂谈
    [03] react 测试
    [02] 前端测试工具集锦
    [01]关于TDD、BDD和DDD的一些看法
    [04]JS获取文件大小方法
    [03]使用阿里RAP搭建前端Mock Server
    [1]区分event对象中的[clientX,offsetX,screenX,pageX]
    [02]a tag只为成button用时候设置href的办法
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/2793209.html
Copyright © 2011-2022 走看看