监控Android系统设定值 [英] Monitor Android system settings values

查看:155
本文介绍了监控Android系统设定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看一个系统设置和得到通知时,它的价值变化。在游标类有一个<一个href="http://developer.android.com/reference/android/database/Cursor.html#setNotificationUri%28android.content.ContentResolver,%20android.net.Uri%29"相对=nofollow> setNotificationUri 方法听起来不错,但它不能正常工作和编码,也觉得很奇怪......那是我所做的:

I want to watch a system setting and get notified when its value changes. The Cursor class has a setNotificationUri method which sounded nice, but it doesn't work and coding it also feels strange... Thats what I did:

    // Create a content resolver and add a listener
    ContentResolver resolver = getContentResolver();
    resolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS | ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, new MyObserver());

    // I somehow need to get an instance of Cursor to use setNotificationUri in the next step...
    Cursor cursor2 = resolver.query(Settings.System.CONTENT_URI, null, null, null, null);

    // For testing purposes monitor all system settings
    cursor2.setNotificationUri(resolver, Settings.System.CONTENT_URI);

监听器:

public class MyObserver implements SyncStatusObserver {

public void onStatusChanged(int which) {
    Log.d("TEST", "status changed, which = " + which);

}
}

嗯,很明显听者得到从来没有所谓,我找不到在logcat中指定的测试标签的条目):(​​用于测试我手动更改亮度从手册中的Andr​​oid设置菜单设置为自动)。任何暗示什么,我做错了什么?任何其他更好的方法来监视Android系统设置?

Well, obviously the listener gets never called, I can't find an entry with the specified TEST tag in logcat ): (For testing I manually changed the brightness setting from manual to automatic in the android settings menu). Any hint what I am doing wrong? Any other, better way to monitor Android system settings?

感谢您的任何提示!

推荐答案

在这里是如何可以做到的,伟大工程:的如何实现一个Android ContentObserver 。注意不是某些设置先写入/当用户presses在他改变一些系统preference屏幕返回键reallly变了!

here is how it can be done, works great: How to implement an Android ContentObserver. note than some settings are first written / reallly changed when the user presses the back key in the system preference screen where he changed something!

这篇关于监控Android系统设定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆