监控Android系统设置值(Monitor Android system settings values)

编程入门 行业动态 更新时间:2024-10-26 19:30:00
监控Android系统设置值(Monitor Android system settings values)

我想观看系统设置,并在其值发生变化时收到通知。 Cursor类有一个setNotificationUri方法,听起来不错,但它不起作用,编码它也感到奇怪......这就是我所做的:

// 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中找到具有指定TEST标签的条目):(为了测试,我在Android设置菜单中手动将亮度设置从手动更改为自动)。 任何暗示我做错了什么? 任何其他更好的方式来监视Android系统设置?

感谢您的任何提示!

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

The listener:

public class MyObserver implements SyncStatusObserver { public void onStatusChanged(int which) { Log.d("TEST", "status changed, which = " + which); } }

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?

Thanks for any hint!

最满意答案

这里是它如何完成,很好用: 如何实现呼叫日志的ContentObserver 。 请注意,当用户在系统首选项屏幕中按下后退键时,某些设置会首先被写入/重新更改!

here is how it can be done, works great: How to implement a ContentObserver for call logs. 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!

更多推荐

本文发布于:2023-07-17 04:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:系统设置   Monitor   Android   values   settings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!