2
ContentObserver在Android 4.0.4中正常工作,可以檢測設置應用程序中所做的更改。但是,當在Android 4.3的它不檢測的變化和服務犯規啓動Android:ContentObserver不能在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(http://developer.android.com/reference/android/provider/Settings.Global.html),所以它可能值得收聽uri提供的內容。 –
是的數據設置已從安全移到全局。 –