检测手机旋转(运动) [英] Detecting phone rotation (movement)
本文介绍了检测手机旋转(运动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是可以检测手机运动就像在图像上是什么时候?什么是这android的名字?
Is this possible to detect when the phone moves like on the image? What is the name of this in android?
是否有处理这种事情无论如何?一年前,我看到了一个应用程序有一个指南针,它是实时的工作。
Is there any event that handle this kind of things? A year ago I saw an App with a compass and it was working in real time.
谢谢!
推荐答案
我希望这code有助于
I hope that this code will help
package com.exercise.AndroidSensorEventListener;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidSensorEventListener extends Activity {
private static SensorManager mySensorManager;
private boolean sersorrunning;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
if(mySensors.size() > 0){
mySensorManager.registerListener(mySensorEventListener, mySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
sersorrunning = true;
Toast.makeText(this, "Start ORIENTATION Sensor", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "No ORIENTATION Sensor", Toast.LENGTH_LONG).show();
sersorrunning = false;
finish();
}
}
private SensorEventListener mySensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
System.out.println("Azimuth: " + String.valueOf(event.values[0]));
System.out.println("Pitch: " + String.valueOf(event.values[1]));
System.out.println("Roll: " + String.valueOf(event.values[2]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if(sersorrunning){
mySensorManager.unregisterListener(mySensorEventListener);
Toast.makeText(AndroidSensorEventListener.this, "unregisterListener", Toast.LENGTH_SHORT).show();
}
}
}
这篇关于检测手机旋转(运动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文