I have a ContentObserver in my fragment observes some data change in Settings.Global:
private ContentObserver mObserver = null;
private Context mContext = null;
......
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContext = getActivity();
......
mObserver = new MyObserver(new Handler());
Uri uri = Settings.Global.getUriFor(Settings.Global.xxx);
mContext.getContentResolver().registerContentObserver(uri, true, mObserver);
......
}
......
@Override
public void onDestroyView() {
if (mObserver != null) {
mContext.getContentResolver().unregisterContentObserver(mObserver);
mObserver = null;
}
}
class MyObserver extends ContentObserver {
MyObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
// do something
}
}
This works fine when the user id is 0. However, when there is another user created, let's say user id is 10, then from Android Device Monitor I can see two processes of my App is running, e.g. com.example.myapp and com.example.myapp(10). And the foreground process is com.example.myapp(10). At this time when there is data change happening in Settings.Global, the onChange() is not triggered for com.example.app(10) whereas it is triggered for com.example.myapp.
Any ideas?
You need to add a parameter on registerContentObserver to listen the changes for all users.
registerContentObserver(uri, true, mObserver, UserHandle.USER_ALL);
and on method onChange() you will check the change on current user.