• 监听短信数据库变化


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

  • 相关阅读:
    iphone/iOS 访问本地数据库sqlite3
    SQLITE3 --详解
    iOS使用MD5
    ASIHTTPRequest实现断点续传
    ios开发
    iOS 5的文件存储策略应对
    由ASIHttpRequest里的block引发的思考
    Blocks编程要点
    [Cocoa]深入浅出Cocoa多线程编程之 block 与 dispatch quene
    ASIHTTPRequest 问题总结
  • 原文地址:https://www.cnblogs.com/fengzhblog/p/2793209.html
Copyright © 2020-2023  润新知