java - 如何在 android 中没有更新值的情况下只获取加速度计 X、Y、Z 值?

标签 java android mobile

您好,我有一个类基本上可以通过单击按钮收集 GPS 数据(经度、纬度和方位)、时间(以 H、M 和 S 表示)和加速度计数据(X、Y、Z)。到目前为止,我已经让 GPS 正常工作(除了 Bearing 出于某种原因只显示 0?我需要为此设置一些特殊权限吗??)以及在单击 Button 时显示的时间,但加速度计X、Y 和 Z 不断变化,因为它们与监听器相关联。有没有像 getX()、getY() 或 getZ() 这样的方法可以在单击按钮时只给我一个 X、Y、Z 读数,而不会每次都改变。到目前为止,这是我的代码:

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

}
}

请尽快帮忙!!!谢谢!

最佳答案

获取值后,只需在 onSensorChanged() 中注销监听器即可。

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

    sm.unregisterListener(this);
}

关于java - 如何在 android 中没有更新值的情况下只获取加速度计 X、Y、Z 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14824844/

相关文章:

java - 使用 aChartEngine 绘制传感器数据的动态图 - Android

java - 为什么我需要覆盖 Java 中的 equals 和 hashCode 方法?

java - Android混淆器: How to set up in Eclipse?

android - IronSource 奖励视频加载

android - 任何人都可以告诉我如何从 android 中的最后一个放大点开始缩小吗?

android - 如何在 Android 中通过 YouTube API 获取用户的 Youtube 播放列表?

android - 如何在模拟器中禁用串行控制台?

flutter - Flutter在轮播中添加具有已知位置的图像

java - Neo4j-reco : Engine FriendsComputingEngine wasn't found on the classpath

java - 关于实现一次性密码系统,我还需要了解什么?