简体   繁体   English

如何在不具有android中更新值的情况下仅获取加速度计X,Y,Z值?

[英]How do I get just the accelerometer X, Y, Z values without having an updating value in android?

Hi I have a class that basically gathers GPS data (Longitude, Lattitude, and Bearing), Time (in H, M, and S), and accelerometer data (X,Y,Z) all on button click. 嗨,我有一类基本上是在单击按钮时收集GPS数据(经度,纬度和方位),时间(以H,M和S表示)和加速度计数据(X,Y,Z)的类。 So far I have gotten the GPS stuff to work (except Bearing for some reason only shows 0?? Do I need to set some special permissions for this??) and the time to show at the instant the Button is clicked, but the accelerometer X,Y,and Z are constantly changing because they are tied with a listener. 到目前为止,我已经使GPS可以正常工作了(“ Bearing”出于某种原因仅显示0?我是否需要为此设置一些特殊权限?)以及单击Button的瞬间显示的时间,但是加速度计X,Y和Z不断变化,因为它们与听众联系在一起。 Is there a method like getX(), getY(), or getZ() that will give me just one X,Y,Z reading on button click without it changing everytime. 是否有类似getX(),getY()或getZ()的方法,该方法在单击按钮时仅读取一次X,Y,Z,而不会每次更改。 Here is my code so far: 到目前为止,这是我的代码:

package com.example.servicetest3;

import java.util.Calendar;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements SensorEventListener {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

protected LocationManager locationManager;    
protected Button retrieveLocationButton;
protected TextView displayView;

//accelerometer vars
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
TextView time;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    displayView = (TextView) findViewById(R.id.displayView);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
    );

    //time
    time = (TextView) findViewById(R.id.time);

    //set up Accelerometer service and its corresponding textViews
    acceleration = (TextView) findViewById(R.id.acceleration);
    sm = (SensorManager) getSystemService(SENSOR_SERVICE);
    accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);

retrieveLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showCurrentLocation();
            int hour = Calendar.getInstance().get(Calendar.HOUR);
            int minute = Calendar.getInstance().get(Calendar.MINUTE);
            int second = Calendar.getInstance().get(Calendar.SECOND);
            time.setText(Integer.toString(hour) + " | " + Integer.toString(minute) + " | " + Integer.toString(second));
        }
});        

}    

protected void showCurrentLocation() {

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) {

        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s \n Bearing: %3$s",
                location.getLongitude(), location.getLatitude(), location.getBearing()
        );
        displayView.setText(message);

        //Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }

}   

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s \n Bearing: %3$s",
                location.getLongitude(), location.getLatitude(), location.getBearing()
        );
        //Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(MainActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(MainActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(MainActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}

//accelerometer
public void onSensorChanged(SensorEvent event) {
   acceleration.setText("X: " + event.values[0] + 
                        "\nY: " + event.values[1] +
                        "\nZ: " + event.values[2]);

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}
}

Please help ASAP!!! 请尽快帮助!!! Thanks! 谢谢!

Just unregister the listener in onSensorChanged() after you get your values. 获取值后,只需在onSensorChanged()注销侦听器onSensorChanged()

public void onSensorChanged(SensorEvent event) {
    acceleration.setText("X: " + event.values[0] + 
                        "\nY: " + event.values[1] +
                        "\nZ: " + event.values[2]);

    sm.unregisterListener(this);
}

暂无
暂无

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

相关问题 Java字符串:如何从字符串“vt X,Y,Z”获取X,Y和Z的值 - Java string: how do I get the value of X, Y, and Z from a string “vt X,Y,Z” 为什么在此代码中出现“值X不是Y的成员”错误? - Why do I get “value X is not a member of Y” error in this code? 当对象 position 在时间轴中移动时,如何获取它的 x 和 y 值? - How do I get the x and y values of an objects position when it is moving in a timeline? 如何将多个对象的x,y,z坐标转换为显示这些对象位置的Android应用程序? - How could I turn coordinates x,y,z coordinates of several objects into an Android app that displays the position of these objects? 如何获取/修改玩家 x、y 和 z 坐标我的世界? - How to get/modify player x,y & z coordinates minecraft? 如何在JScrollPane中获取图像的X和Y位置 - How do I get the X and Y location of an image in a JScrollPane 两个不同的jar x,y依赖于z的不同版本。 用户如何在应用程序中同时拥有x,y? - Two different jars x,y having dependency on different versions of z. How user can have both x,y in his application? 如何从onSensorChanged到ThreatForLoopA类获取加速度计值? - How can i get accelerometer values from onSensorChanged to class ThreatForLoopA? 番石榴CheckedFuture <X,Y> 映射到CheckedFuture <Z,Y> 无阻碍 - Guava CheckedFuture<X,Y> map to CheckedFuture <Z,Y> without blocking 如何在Android应用程序中设置x,y值? - How to set x,y value in android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM