不同手机方向上的Android指南针
创始人
2025-01-09 17:01:20
0

以下是一个使用Android Studio编写的简单示例代码,演示如何获取不同手机方向上的指南针:

  1. 在AndroidManifest.xml文件中添加以下权限:


  1. 创建一个名为MainActivity的Java类,并在onCreate方法中添加以下代码:
import android.Manifest;
import android.content.pm.PackageManager;
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.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity implements SensorEventListener, LocationListener {

    private TextView compassTextView;
    private SensorManager sensorManager;
    private Sensor accelerometer;
    private Sensor magnetometer;
    private float[] lastAccelerometer = new float[3];
    private float[] lastMagnetometer = new float[3];
    private boolean isAccelerometerSet = false;
    private boolean isMagnetometerSet = false;
    private float[] rotationMatrix = new float[9];
    private float[] orientation = new float[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        compassTextView = findViewById(R.id.compassTextView);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        startLocationUpdates();
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == accelerometer) {
            System.arraycopy(event.values, 0, lastAccelerometer, 0, event.values.length);
            isAccelerometerSet = true;
        } else if (event.sensor == magnetometer) {
            System.arraycopy(event.values, 0, lastMagnetometer, 0, event.values.length);
            isMagnetometerSet = true;
        }

        if (isAccelerometerSet && isMagnetometerSet) {
            SensorManager.getRotationMatrix(rotationMatrix, null, lastAccelerometer, lastMagnetometer);
            SensorManager.getOrientation(rotationMatrix, orientation);
            float azimuth = (float) Math.toDegrees(orientation[0]);
            if (azimuth < 0) {
                azimuth += 360;
            }

            compassTextView.setText("Azimuth: " + azimuth);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onLocationChanged(Location location) {
        // 在这里更新位置信息,例如更新经纬度
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    private void startLocationUpdates() {
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }
}
  1. 在res/layout文件夹中创建一个名为activity_main.xml的XML布局文件,并添加以下内容:



    

相关内容

热门资讯

科普攻略!德普之星辅助器app... 科普攻略!德普之星辅助器app,we poker辅助器,德州论坛(有挂软件)是一款可以让一直输的玩家...
重大科普!佛手在线大菠萝智能辅... 重大科普!佛手在线大菠萝智能辅助器,wepoker作弊辅助,分享教程(有挂软件);原来确实真的有挂(...
一分钟教会你!wepoker怎... 一分钟教会你!wepoker怎么增加运气,epoker透视,切实教程(有挂透视)1、点击下载安装,微...
六分钟了解!hhpoker有辅... 六分钟了解!hhpoker有辅助吗,wepoker国外版透视,扑克教程(有挂技巧)科技教程也叫必备教...
我来教大家!wepoker辅助... 我来教大家!wepoker辅助透视,wepoker免费脚本弱密码,详细教程(有挂透明);wepoke...
记者发布!wpk辅助,德普之星... 记者发布!wpk辅助,德普之星透视辅助软件激活码,解密教程(有挂辅助);亲真的是有正版授权,小编(透...
揭秘攻略!aapoker万能辅... 《揭秘攻略!aapoker万能辅助器,hhpoker真的假的,揭秘教程(有挂教程)》 aapoker...
重大通报!sohoo poke... 自定义sohoo poker辅助器系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用...
三分钟了解!wpk辅助器,hh... 1、三分钟了解!wpk辅助器,hhpoker免费辅助器,必赢教程(有挂神器);详细教程。2、hhpo...
玩家必看攻略!wejoker私... 玩家必看攻略!wejoker私人辅助软件,智星德州可以透视吗,透明挂教程(有挂技巧)关于智星德州可以...