Android中的全局监听方式
2020-10-13 本文已影响0人
WangRain1
1.使用Settings.system.get/set
1.监听Context.getContentResolver().registerContentObserver(Settings.System.getUriFor(DEF_SENSOR_STATE), false, ContentObserver);
2.获取Settings.System.getInt(getApplicationContext().getContentResolver(), DEF_SENSOR_STATE, -1); // 获取
3.设置 Settings.System.putInt(getApplicationContext().getContentResolver(), "sensor_state", 2);
4.也可以notify :Context().getContentResolver().notifyChange(SLEEP_DURATION_URI, null);
根据contentprovider 生成对应的uri 就能监听 uri 对应的值 的改变
ContentObserver callback中查询 Cursorcursor=context.getContentResolver().query(STEP_HOURS_URI,507 null,null,null,null);508 inthours=cursor.getExtras().getInt(STEP_HOURS_PATH,0);