【发布时间】:2013-03-29 10:06:26
【问题描述】:
我在我的应用程序中使用加速度计,但在多个设备上传感器轴不同(取决于默认设备方向是纵向还是横向)。 在 AndroidMaifest.xml 中:
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
...
android:screenOrientation="portrait"
如何从本机代码获取默认设备方向?
【问题讨论】:
我在我的应用程序中使用加速度计,但在多个设备上传感器轴不同(取决于默认设备方向是纵向还是横向)。 在 AndroidMaifest.xml 中:
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
...
android:screenOrientation="portrait"
如何从本机代码获取默认设备方向?
【问题讨论】:
在您的 OnCreate()(或您进行初始化的函数)中检查设备的当前方向,然后根据方向为您的传感器设置轴。
按照以下链接查看如何获取 android 设备的当前方向:
Check orientation on Android phone
how to detect orientation of android device?
希望对您有所帮助。如果没有,请发表评论以分享您的问题的更多详细信息。
【讨论】:
如果有人仍然对获得初始/默认屏幕方向的纯原生解决方案感兴趣,请使用下面的代码。
有关相关 JAVA 方法的更多信息:
https://developer.android.com/reference/android/view/Display.html#getRotation()
int get_inital_screen_orientation(struct android_app * app){
JavaVM *lJavaVM = app->activity->vm;
JNIEnv *lJNIEnv = app->activity->env;
jobject n_instance = app->activity->clazz;
lJavaVM->AttachCurrentThread(&lJNIEnv, 0);
int rotation = -1;
if (lJNIEnv) {
jclass c_clazz = lJNIEnv->GetObjectClass(n_instance);
jclass c_windowManager = lJNIEnv->FindClass("android/view/WindowManager");
jclass c_display = lJNIEnv->FindClass("android/view/Display");
jmethodID getWindowManager = lJNIEnv->GetMethodID(c_clazz, "getWindowManager", "()Landroid/view/WindowManager;");
jmethodID getDefaultDisplay = lJNIEnv->GetMethodID(c_windowManager,"getDefaultDisplay","()Landroid/view/Display;");
jmethodID getRotation = lJNIEnv->GetMethodID(c_display, "getRotation", "()I");
jobject windowManager = lJNIEnv->CallObjectMethod(n_instance, getWindowManager);
jobject display = lJNIEnv->CallObjectMethod(windowManager, getDefaultDisplay);
rotation = lJNIEnv->CallIntMethod(display, getRotation);
lJavaVM->DetachCurrentThread();
}
return rotation;
}
【讨论】: