【问题标题】:Android: ContentObserver not working in android 4.3Android:ContentObserver 在 android 4.3 中不起作用
【发布时间】:2013-10-13 09:40:29
【问题描述】:

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



}

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

}

谢谢, 萨希尔

【问题讨论】:

标签: java android


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多