Android 如何从状态栏检测方向变化

Android 如何从状态栏检测方向变化,android,android-intent,orientation,broadcastreceiver,listener,Android,Android Intent,Orientation,Broadcastreceiver,Listener,在状态栏下,有五个按钮-Wifi、蓝牙、GPS、静音模式和自动旋转。我还没有找到一种方法来检测用户何时单击自动旋转按钮 我所做的是提供一种服务,可以监听方向的变化,例如: public class MyService extends Service { @Override public void onCreate() { super.onCreate(); IntentFilter intentFilter = new IntentFilter();

在状态栏下,有五个按钮-Wifi、蓝牙、GPS、静音模式和自动旋转。我还没有找到一种方法来检测用户何时单击自动旋转按钮

我所做的是提供一种服务,可以监听方向的变化,例如:

public class MyService extends Service {

    @Override
    public void onCreate() {
       super.onCreate();
       IntentFilter intentFilter = new IntentFilter();
       intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
       this.registerReceiver(myReceiver, intentFilter);     
    }

    public BroadcastReceiver myReceiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
          if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) {
             // Show a message about orientation changes.
             // This method is only called when the phone is physically rotated.
          }
       }
    }  
}
上述BroadcastReceiver仅在手机进行物理旋转的情况下工作,但如果在没有对手机进行任何物理旋转的情况下单击自动旋转按钮,则永远不会调用BroadcastReceiver方法

我也尝试过OrientationEventListener,但行为与BroadcastReceiver方法相同

为了设置方向,我使用了以下方法:

Settings.System.getInt(context.getContentResolver(), "accelerometer_rotation")
所以我知道有一种方法可以询问系统设置。是否存在阻止系统设置[方向]更改的意图?我浏览了Android文档,但找不到一个——也许我找错地方了,或者根本就没有这个意图

是否有人知道如何捕获对状态栏中“自动旋转”按钮的更改

更新1:我正在使用的设备有:三星Galaxy S3、三星Galaxy S2、三星Galaxy S和三星Galaxy Ace等。所有这些设备都运行库存ROM,并且在状态栏下有自动旋转按钮

更新2:有人建议使用ContentObserver来监听系统设置的更改,例如:

public class SettingsContentObserver extends ContentObserver {
    public SettingsContentObserver(Handler handler) {
       super(handler);
    }

    @Override
    public boolean deliverSelfNotifications() {
       return super.deliverSelfNotifications(); 
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Logging.writeDebug("Settings change detected: " + selfChange);
    }

    public void registerContentObserver(Context context) {
        context.getContentResolver().registerContentObserver(Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), true, this);
    }

    public void unregisterContentObserver(Context context) {
       context.getContentResolver().unregisterContentObserver(this);
    }
}

但是,我发现
onChange
方法中的
selfChange
变量总是返回
false

,我想您将不得不监听
设置.System.accelerator\u ROTATION
。 我还没试过,但这应该行得通

getContentResolver().registerContentObserver(Settings.System.getUriFor
(Settings.System.ACCELEROMETER_ROTATION),
true,contentObserver);
编辑: 就像接收广播接收器一样,内容观察者也会发生变化。因此,无论何时单击按钮,都会在此处收到回调

private ContentObserver contentObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
           // show a toast here
        }
};

股票AOSP Android没有您在状态栏中描述的按钮。如果您提到正在使用的设备,这可能会有所帮助。@adamp:请参阅我上面问题中的更新信息。单击该按钮是否会导致实际方向的更改(是否重置为默认/自然方向?),或者只是将设备锁定为当前方向?如果是后者,听起来你所观察到的行为就像预期的那样——你没有收到方向改变事件,因为方向已经被锁定,并且没有改变。你想做什么?@adamp:在手机进行物理定位之前,没有方向是“可见”的,但是当点击按钮时,它是启用的。单击自动旋转按钮与[在ICS 4.0.3上]单击设置|显示|自动旋转屏幕复选框相同。我正在尝试检测何时启用/禁用方向以通过我的应用程序中的TextView对象提供消息。我希望这是有意义的。如果您想监视该设置的更改。@@nandeesh:您能详细说明您的答案吗?以上仅用于注册内容观察者。使用什么方法从内容观察者获得结果?你的回答不清楚。谢谢你的回答,@nandeesh。但是,我将ContentObserver分离为一个单独的类,并在主活动中调用它(请参见上面的更新2)。我注意到
onChange
方法中的
selfChange
变量总是返回
false
。这是不对的,是吗?selfchange表示,你自己的应用程序是否改变了它。在您的情况下,由于您正在状态栏中更改它,因此将返回false。您必须再次查询uri以了解当前状态您的意思是在
onChange
方法中再次执行
Settings.System.accelerator\u ROTATION
?是的,执行此设置.System.getInt(context.getContentResolver(),Settings.System.accelerator\u ROTATION)以获取当前值