компас не работает на некоторых устройствах Android - PullRequest
0 голосов
/ 30 мая 2018

ЭТОТ КОД - ПРИЛОЖЕНИЕ ANDROID КОМПАСА ЭТОТ КОМПАС НЕ ПОДДЕРЖИВАЕТСЯ В НЕКОТОРЫХ УСТРОЙСТВАХ ПОЖАЛУЙСТА, ПОМОГИТЕ МНЕ

private SensorManager SensorManage;
// define the compass picture that will be use
private ImageView compassimage;
// record the angle turned of the compass picture
private float DegreeStart = 0f;
TextView DegreeTV;

  compassimage = (ImageView) findViewById(R.id.compass_image);
    // TextView that will display the degree
    DegreeTV = (TextView) findViewById(R.id.DegreeTV);
    // initialize your android device sensor capabilities
    SensorManage = (SensorManager) getSystemService(SENSOR_SERVICE);
}

ЭТО МЕТОД ДЛЯ КОМПАСА

 @Override
protected void onPause() {
    super.onPause();
    // to stop the listener and save battery
    SensorManage.unregisterListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    // code for system's orientation sensor registered listeners
    SensorManage.registerListener(this, SensorManage.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void onSensorChanged(SensorEvent event) {
    // get angle around the z-axis rotated
    float degree = Math.round(event.values[0]);
    DegreeTV.setText("Heading: " + Float.toString(degree) + " degrees");
    // rotation animation - reverse turn degree degrees
    RotateAnimation ra = new RotateAnimation(
            DegreeStart,
            -degree,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    // set the compass animation after the end of the reservation status
    ra.setFillAfter(true);
    // set how long the animation for the compass image will take place
    ra.setDuration(210);
    // Start animation of compass image
    compassimage.startAnimation(ra);
    DegreeStart = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // not in use
}
}

Ответы [ 2 ]

0 голосов
/ 30 мая 2018

Некоторые устройства не поддерживают компас, у меня фаза такая же проблема.Тогда я проверю, поддерживает ли компас поддержку или нет в устройстве для приведенного ниже кода.

private SensorManager SensorManage ;
private Sensor sensor;

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

    this.SensorManage = (SensorManager) getSystemService(SENSOR_SERVICE);
    this.sensor = this.SensorManage.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    if (this.sensor != null) {
        SensorManage.registerListener(this, SensorManage.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME);
    } else {
        Toast.makeText(this, "Compass Not Supported", Toast.LENGTH_LONG).show();
    }
}

Надеюсь, это поможет вам!

0 голосов
/ 30 мая 2018

Посетите здесь, это простой Android-компас.Вы можете следить за его реализацией.https://github.com/klein-artur/Simple-Android-Compass-Assistant

Пример кода:

@Override
public void onNewSmoothedDegreesToNorth(float degrees) {

    final RotateAnimation ra = new RotateAnimation(
            currentDegree,
            degrees,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f);
    ra.setDuration(210);
    ra.setFillAfter(true);

    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            compass.startAnimation(ra);
        }
    });

    currentDegree = degrees;    
}

В противном случае, посетите здесь для получения более подробной информации https://developer.android.com/reference/android/hardware/SensorManager

Надеюсь, это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...