Android中的全局监听方式
发布网友
发布时间:2023-07-09 22:55
我来回答
共1个回答
热心网友
时间:2024-03-05 00:02
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中查询 Cursor cursor = context . getContentResolver (). query ( STEP_HOURS_URI , 507 null , null , null , null ); 508 int hours = cursor . getExtras (). getInt ( STEP_HOURS_PATH ,0);
热心网友
时间:2024-03-05 00:03
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中查询 Cursor cursor = context . getContentResolver (). query ( STEP_HOURS_URI , 507 null , null , null , null ); 508 int hours = cursor . getExtras (). getInt ( STEP_HOURS_PATH ,0);