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

  • 相关阅读:
    ES6/5比较
    Javascript中的var和let
    git中remotes/origin/HEAD指向的分支丢失
    js实用篇之数组、字符串常用方法
    JS设计模式一:单例模式
    Linux C 面试题总结 .
    深入理解javascript原型和闭包(15)——闭包
    一些有意思的面试题(持续更新) .C语言编程技巧札记
    一个三流大学生的北京三年 .
    C 字节对齐.我的算法学习之路
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/2793209.html
Copyright © 2011-2022 走看看