【发布时间】: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");
}
谢谢, 萨希尔
【问题讨论】:
-
您在寻找什么设置?在 Android 4.2 中,许多人移至 Settings.Global (developer.android.com/reference/android/provider/…),因此可能值得聆听那里提供的内容 uri。
-
是的,数据设置已从安全移至全局。