2

ContentObserver 在 android 4.0.4 中工作正常,它检测到设置应用程序中所做的更改。但是在 android 4.3 下它没有检测到变化并且服务没有启动

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



}

我的服务.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");

}

谢谢, 萨希尔

4

1 回答 1

2

从 Android 4.2开始,其中一些设置已移至Settings.Global- 所以也请收听那里提供的内容 Uri。

于 2013-10-16T08:48:26.920 回答