[英]compass not working some android devices
THIS CODE IS A ANDROID COMPASS APPLICATION THIS COMPASS NOT SUPPORTED IN SOME DEVICES PLEASE HELP ME 此代码是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);
}
THIS IS A METHOD FOR COMPASS 这是指南针的方法
@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
}
}
Some Devices are not support compass, I have phase same problem. 有些设备不支持指南针,我有相位相同的问题。 Then I will check compass is support or not in device for below code.
然后,我将检查以下代码是否支持罗盘。
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();
}
}
Hope this will be help you! 希望这会对您有所帮助!
Visit here, it's a simple android compass. 访问这里,这是一个简单的Android指南针。 You can follow it's implementation.
您可以遵循它的实现。 https://github.com/klein-artur/Simple-Android-Compass-Assistant
https://github.com/klein-artur/Simple-Android-Compass-Assistant
Sample code: 样例代码:
@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;
}
Otherwise, visit here for more details https://developer.android.com/reference/android/hardware/SensorManager 否则,请访问此处以获取更多详细信息https://developer.android.com/reference/android/hardware/SensorManager
Hope this helps you. 希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.