• 监听短信数据库变化


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

  • 相关阅读:
    Dubbo集群配置和官方文档
    Lock wait timeout exceeded
    Too many connections
    mybatis 批量更新 Parameter '__frch_item_0' not found. Available parameters are [list]
    base64文件转MultipartFile文件
    base64之js压缩图片
    NotSupportedError Only secure origins are allowed
    安装MySQL时候最后一步报无法定位程序输入点fesetround于动态链接库MSVCR120.dll
    2018年总结
    java开发之多线程基础篇
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/2793209.html
Copyright © 2020-2023  润新知