//观察屏幕旋转设置变化,类似于注册动态广播监听变化机制
private class RotationObserver extends ContentObserver {
ContentResolver mResolver;
public RotationObserver(Handler handler) {
super(handler);
mResolver = App.context.getContentResolver(); //这里是application里的context
}
//屏幕旋转设置改变时调用
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
//更新按钮状态
}
public void startObserver() {
mResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), false, this);
}
public void stopObserver() {
mResolver.unregisterContentObserver(this);
}
}
提示:配合lifecycle使用就可以抽离activity做很多事情了,使得activity代码更为简洁
网友评论