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.
Thanks!
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.
Thanks!
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();
}
}
}
If you are familiar with RxJava and Reactive Programming, you can use my open-source library called ReactiveSensors. It's a wrapper around SensorManager
from Android SDK and allows you to monitor sensor changes emitted as an observable stream.
You can use the library in the following way:
add the following dependency to your build.gradle
file:
dependencies {
compile 'com.github.pwittchen:reactivesensors:0.1.2'
}
and then, you can use library in your code:
new ReactiveSensors(this).observeSensor(Sensor.TYPE_ORIENTATION)
.subscribeOn(Schedulers.computation())
.filter(ReactiveSensorEvent.filterSensorChanged())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<ReactiveSensorEvent>() {
@Override public void call(ReactiveSensorEvent reactiveSensorEvent) {
SensorEvent event = reactiveSensorEvent.getSensorEvent();
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
String format = "orientation sensor readings:\n x = %f\n y = %f\n z = %f";
String message = String.format(format, x, y, z);
tvSensor.setText(message);
}
});
In your case, you can use Sensor.TYPE_ORIENTATION
, but it works with all kinds of sensors available on Android devices.
Full working sample code is available at: https://github.com/pwittchen/ReactiveSensors/blob/master/app/src/main/java/com/github/pwittchen/reactivesensors/app/samples/OrientationActivity.java
Detailed description of the library, its source code and sample working application is available at: https://github.com/pwittchen/ReactiveSensors
You will need something like a SensorListener
private final SensorListener sl = new SensorListener() {
public void onSensorChanged(int sensor, float[] values) {
}
public void onAccuracyChanged(int sensor, int accuracy) {
}
};
Then register it:
// Locate the SensorManager using Activity.getSystemService
SensorManager sm;
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
// Register your SensorListener
sm.registerListener(sl, SensorManager.SENSOR_ORIENTATION SensorManager.SENSOR_DELAY_NORMAL);
And out of this you will get al the information you need...
Reference:
Take a look at motion sensors: http://developer.android.com/guide/topics/sensors/sensors_motion.html