【问题标题】:How to detect orientation change from the statusbar如何从状态栏检测方向变化
【发布时间】:2012-08-19 18:04:23
【问题描述】:

在状态栏下方,有五个按钮 - 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只有在手机物理旋转的情况下才会起作用,但是如果在手机没有任何物理旋转的情况下点击了Auto Rotation按钮,那么BroadcastReceiver方法永远不会被调用。

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

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

Settings.System.getInt(context.getContentResolver(), "accelerometer_rotation")

所以我知道有一种方法可以询问系统设置。是否有陷阱系统设置的[方向]变化的意图?我浏览了 Android 文档,但找不到 - 也许是我找错地方了,或者没有 Intent。

有谁知道如何捕捉状态栏中“自动旋转”按钮的变化?

更新 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

【问题讨论】:

  • Stock AOSP Android 没有您在状态栏中描述的按钮。如果您提及您正在使用的设备,这可能会有所帮助。
  • @adamp:请参阅上面我的问题中的更新信息。
  • 单击该按钮是否会导致实际的方向更改(是否重置为默认/自然方向?)还是只是将设备锁定到当前方向?如果是后者,听起来您正在观察的行为正在按预期工作 - 您没有收到方向更改事件,因为方向已被锁定并且它没有改变。你想完成什么?
  • @adamp:在手机物理定向之前,没有方向是“可见的”,但是当单击按钮时它仍然启用。单击 Auto Rotation 按钮与 [在 ICS 4.0.3 上] 单击 Settings |显示 |自动旋转屏幕复选框。我试图检测何时启用/禁用方向以通过我的应用程序中的 TextView 对象提供消息。我希望这是有道理的。
  • 如果您想监控该设置的更改:stackoverflow.com/questions/6190779/…

标签: android android-intent orientation broadcastreceiver listener


【解决方案1】:

我猜你将不得不收听Settings.System.ACCELEROMETER_ROTATION。 没有尝试过,但这应该可以。

getContentResolver().registerContentObserver(Settings.System.getUriFor
(Settings.System.ACCELEROMETER_ROTATION),
true,contentObserver);

编辑: 就像 BroadcastReceiver 的 Onrecieve 一样,contentobserver 也有一个 onchange。因此,无论何时单击按钮,您都会在此处收到回调。

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

【讨论】:

  • @@nandeesh:你能详细说明你的答案吗?以上仅用于注册内容观察者。从内容观察者那里得到结果的方法是什么?从您的回答中看不清楚。
  • 感谢您的回复,@nandeesh。但是,我将 ContentObserver 分离到一个单独的类中并在主要活动中调用它(参见上面的更新 2)。我注意到onChange 方法中的selfChange 变量总是返回false。这不对吧?
  • selfchange 表示,是否你自己的应用程序改变了它。在您的情况下,由于您在 statusbar 中更改它,这将返回 false。您将不得不再次查询 uri 以了解当前状态
  • 您的意思是在onChange 方法中再次执行Settings.System.ACCELEROMETER_ROTATION
  • 是的,这样做 Settings.System.getInt(context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION) ,以获取当前值
猜你喜欢
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-28
相关资源
最近更新 更多