每个人..我是Android新手,我想了解加速度计和陀螺仪。 如何在Android应用程序中使用它们以及它是如何工作的。我尝试了一个简单的演示应用程序来检查移动设备上是否找到传感器。但它没有找到.. Plz引导我使用陀螺仪和加速度计。
这是我的演示应用类
public class MainActivity extends Activity {
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null)
{
Toast.makeText(getApplicationContext(), "Found", 9).show();
// Sensor FOUND
}
else
{
Toast.makeText(getApplicationContext(), "NOt...Found", 9).show();
//Sensor NOT FOUND
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
提前完成..
答案 0 :(得分:3)
你可以在陀螺仪中找到适用于android的this链接
和accelerometer
以此
public class yourActivity extends Activity implements SensorEventListener{
private SensorManager sensorManager;
double ax,ay,az; // these are the acceleration in x,y and z axis
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
ax=event.values[0];
ay=event.values[1];
az=event.values[2];
}
}
}