有关ContentObserver 的用法 不指定

Heck post in 编程杂谈 2010/10/15 14:53 评论(0) 阅读(1220) | |
public class ScreenTest extends Activity {    
        
    class SmsContent extends ContentObserver{    
        private Cursor cursor = null;    
        public SmsContent(Handler handler) {    
            super(handler);    
        }    
            
        /**  
         * @Description 当短信表发送改变时,调用该方法    
         *              需要两种权限  
         *              android.permission.READ_SMS读取短信  
         *              android.permission.WRITE_SMS写短信  
         * @Author Snake  
         * @Date 2010-10-12  
         */  
        @Override  
        public void onChange(boolean selfChange) {    
            // TODO Auto-generated method stub    
            super.onChange(selfChange);    
            //读取收件箱中指定号码的短信    
            cursor = managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id", "address", "read"}, " address=? and read=?", new String[]{"12345678901", "0"}, "date desc");

                
            if (cursor != null){    
                ContentValues values = new ContentValues();    
                values.put("read", "1");        //修改短信为已读模式    
                cursor.moveToFirst();    
                while (cursor.isLast()){    
                    //更新当前未读短信状态为已读    
                    getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?", new String[]{""+cursor.getInt(0)});    
                    cursor.moveToNext();    
                }    
            }    
        }    
    }    
        
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);    
        SmsContent content = new SmsContent(new Handler());    
        //注册短信变化监听    
        this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);    
    }    
}


来源:Heck's Blog
地址:http://www.hecks.tk/android-ContentObserver-what-use/
转载时须以链接形式注明作者和原始出处及本声明,否则将追究法律责任,谢谢配合!