简体   繁体   English

android-为什么我无法获得设备定位?

[英]android-why I can't get device orientation?

I want to make an app which shows device orientation over 3 axises while the user clicks on a button, but the result always is 0.0|0.0|0.0 (no rotation) , why? 我想制作一个可以在用户单击按钮时在3个轴上显示设备方向的应用程序,但结果始终是0.0 | 0.0 | 0.0(不旋转),为什么?

 package com.example.newp;

 import android.app.Activity;
 import android.hardware.SensorManager;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Toast;

 public class FullscreenActivity extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_fullscreen);
}

public void onButtonClick(View view) {
    float[] mGravs = new float[3];
    float[] mGeoMags = new float[3];
    float[] mRotationM = new float[9];
    float[] mInclinationM = new float[9];
    float[] mOrientation = new float[3];
    SensorManager.getRotationMatrix(mRotationM, mInclinationM, mGravs,
            mGeoMags);
    SensorManager.getOrientation(mRotationM, mOrientation);
    Toast.makeText(
            this,
            String.valueOf(mOrientation[0]) + "|"
                    + String.valueOf(mOrientation[1]) + "|"
                    + String.valueOf(mOrientation[2]), Toast.LENGTH_LONG)
            .show();
}
 }

You are feeding in the wrong values. 您输入的值有误。 mGravs is [0.0, 0.0, 0.0] and so is mGeoMags. mGravs是[0.0,0.0,0.0],mGeoMags也是如此。 These values should come from sensor TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD respectively. 这些值应分别来自传感器TYPE_ACCELEROMETERTYPE_MAGNETIC_FIELD You can use the values returned by SensorEvent of these sensors. 您可以使用这些传感器的SensorEvent返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-如何获取当前方向? - android - how can I get current orientation? 无法获取唯一的Android设备ID - Can't get the unique Android device ID Android:为什么我不能编程配对蓝牙设备,即使使用BLUETOOTH_PRIVILEGED和setPairingConfirmation - Android: Why can't I programmaticaly pair to a bluetooth device, even with BLUETOOTH_PRIVILEGED and setPairingConfirmation Android上的设备方向 - Device orientation on Android 当我的设备将方向更改为上下颠倒(180度)时,如何获得通知? - How can I get a notification when my device changes orientation to portrait upside down (180 degrees)? 我无法在 API &lt;=22(使用 Android Studio 和 Unity)的设备中启动手电筒。为什么? - I can't start torch in device with API <=22(Used Android Studio and Unity) .why? 更改方向Android设备 - Changing orientation Android device 为什么我在Android设备上看不到布局更改并得到未解决的参考错误? - Why don't I see layout changes on Android device and get unresolved reference error? 为什么当我在android中获取图像的坐标时,它会根据设备而变化? - Why, when i get the coordinates of an image in android, it changes according to the device? 为什么我不能创建从 Android 设备到 PC 的 tcp 套接字 - Why can not I create tcp socket from Android device to PC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM