我想知道以编程方式注册广播接收器的最佳实践/方法是什么.我想根据用户的选择注册特定的接收器.
由于注册是通过清单文件完成的,我想知道是否有一种正确的方法可以在代码中实现这一点.
我想观看系统设置,并在其值发生变化时收到通知.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);
Run Code Online (Sandbox Code Playgroud)
听众:
public class MyObserver implements SyncStatusObserver {
public void onStatusChanged(int which) {
Log.d("TEST", "status changed, which = " + which);
}
} …Run Code Online (Sandbox Code Playgroud)