美文网首页
Android中的全局监听方式

Android中的全局监听方式

作者: WangRain1 | 来源:发表于2020-10-13 17:19 被阅读0次

    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);

    相关文章

      网友评论

          本文标题:Android中的全局监听方式

          本文链接:https://www.haomeiwen.com/subject/vsnvpktx.html