对系统数据库表变化的监听
1.注册监听
this.getContentResolver().registerContentObserver(CallLog.CONTENT_URI, true, myContentObserver);
2.
/** * 用来实现对通话记录表变化的监听 监听到变化的时候发送Message * * */ private ContentObserver myContentObserver = new ContentObserver( new Handler() { }) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); System.out.println("---------------监听到表变化!"); Message ms = new Message(); handler.dispatchMessage(ms);//发送消息给Handler } };
3.
private final Handler handler = new Handler() { @Override public void handleMessage(Message msg) {//处理消息 // if (willDo != null) // { // System.out.println("removeCallbacks一次!"); // this.removeCallbacks(willDo);//从队列中移除 // // } // this.postDelayed(willDo, 2000);//怎么理解???????2秒内如果接受到新的消息 System.out.println("外面一次!"); if (willDo != null && flag == true) { this.post(willDo); System.out.println("里面一次!"); flag = false; Timer timer = new Timer();// 设置一个定时器,如果3秒后再将flag设置为true,才能继续监听改变 timer.schedule(new TimerTask() { @Override public void run() { flag = true; } }, 3000); } } };
4.
private Runnable willDo = new Runnable() { public void run() { initCallLogs();//对表变化所执行的一些操作 System.out.println("WillDo执行一次!"); } };
相关推荐
CoderToy 2020-11-16
技术之博大精深 2020-10-16
emmm00 2020-11-17
bianruifeng 2020-11-16
云中舞步 2020-11-12
世樹 2020-11-11
暗夜之城 2020-11-11
张荣珍 2020-11-12
amienshxq 2020-11-14
ASoc 2020-11-14
yungpheng 2020-10-19
loveyouluobin 2020-09-29
尘封飞扬 2020-09-29
Coder技术文摘 2020-09-29
lbyd0 2020-11-17
BigYellow 2020-11-16
sushuanglei 2020-11-12
我心似明月 2020-11-09