2

ContentObserver works fine in android 4.0.4 where it detects changes made in settings app. But when under android 4.3 it doesnt detect the change and the service doesnt start

SettingsContentObserver.java:

public class SettingsContentObserver extends ContentObserver
{

private Context context;


public SettingsContentObserver(Handler handler, Context applicationContext) {
    // TODO Auto-generated constructor stub
    super(handler);
    this.context = applicationContext;
}

@Override
public boolean deliverSelfNotifications()
{
    return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange)
{
    super.onChange(selfChange);

  System.out.println("Change detected");
  Intent i = new Intent(context, MyService.class);
  context.startService(i);
}



}

MyService.java:

public void onCreate() {
 SettingsContentObserver mSettingsContentObserver = new SettingsContentObserver(new Handler(), getApplicationContext());
        getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
    System.out.println("Observer registered");
}

public void onStart(Intent intent, int startId) {
            System.out.println("Service works");

}

Thanks, Sahil

Sahil Lombar
  • 145
  • 1
  • 11
  • 2
    What settings are you looking for? In Android 4.2, many moved over to Settings.Global (http://developer.android.com/reference/android/provider/Settings.Global.html) so it might be worth listening to the content uri provided there as well. – Alex Curran Oct 13 '13 at 10:02
  • Yes the data setting was moved from secure to global. – Sahil Lombar Oct 15 '13 at 12:02

1 Answers1

2

Some of these settings have been moved to Settings.Global as of Android 4.2 - so listen to the content Uri provided there as well.

Alex Curran
  • 8,818
  • 6
  • 49
  • 55