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

  • 相关阅读:
    【插队问题-线段树-思维巧妙】【poj2828】Buy Tickets
    【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst
    HDU 6156 Palindrome Function 数位DP
    HDU 6154 CaoHaha's staff 思维 找规律
    Educational Codeforces Round 18 A B 码力 比赛
    Codeforces 815 B Karen and Test 杨辉三角 组合数学
    Codeforces 815 A Karen and Game 贪心
    POJ 1006 Biorhythms 中国剩余定理 数论
    Codeforces 818 E Card Game Again 线段树 思维
    Educational Codeforces Round 24-A B C D 思维
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/2793209.html
Copyright © 2011-2022 走看看