我正在尝试检测我的设备上的屏幕旋转。我有以下...
void handle_cmd(android_app *pApp, int32_t cmd) {
aout << "Got Command " << cmd << std::endl;
switch (cmd) {
case APP_CMD_INIT_WINDOW:
// A new window is created, associate a renderer with it. You may replace this with a
// "game" class if that suits your needs. Remember to change all instances of userData
// if you change the class here as a reinterpret_cast is dangerous this in the
// android_main function and the APP_CMD_TERM_WINDOW handler case.
pApp->userData = new Renderer(pApp);
break;
case APP_CMD_TERM_WINDOW:
// The window is being destroyed. Use this to clean up your userData to avoid leaking
// resources.
//
// We have to check if userData is assigned just in case this comes in really quickly
if (pApp->userData) {
//
auto *pRenderer = reinterpret_cast<Renderer *>(pApp->userData);
pApp->userData = nullptr;
delete pRenderer;
}
break;
case APP_CMD_CONFIG_CHANGED:
// The current device configuration has changed.
aout << "Device configuration has changed (e.g., orientation)" << std::endl;
break;
default:
break;
}
}
当我在模拟器中运行并旋转屏幕(然后单击图标;-))时,我在 logcat 中看不到
Device configuration has changed
。如果我检查触发的事件,我会发现......
... AO com.example.testapp D Got Command 13
... AO com.example.testapp D Got Command 2
... AO com.example.testapp D Got Command 14
... AO com.example.testapp D Got Command 12
... AO com.example.testapp D Got Command 15
... AO com.example.testapp D Got Command 10
... AO com.example.testapp D Got Command 11
... AO com.example.testapp D Got Command 16
... AO com.example.testapp D Got Command 16
... AO com.example.testapp D Got Command 1
... AO com.example.testapp D Got Command 3
... AO com.example.testapp D Got Command 4
... AO com.example.testapp D Got Command 6
... AO com.example.testapp D Got Command 16
这也没有显示
APP_CMD_CONFIG_CHANGED
应该是 8。那么我应该使用这些事件中的哪一个来注册状态更改?还有一个页面显示每个枚举的数值吗?
这仅适用于 Android Oreo 之前的版本,并且在问题跟踪器上它也被标记为“无法修复”。
建议的解决方法是在 Java 中覆盖
onConfigurationChanged()
,然后调用 JNI。