Please Implement SensorEventListener in your Activity
Declare below Variables or Make Objects of Below Class;
private SensorManager sensorManager;
private long lastUpdate;
Boolean bool = false, bool1 = false;
Write This three Line into Your onCreate Method:-
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
lastUpdate = System.currentTimeMillis();
Add Following Methods into our Activity:-
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
System.out.println("accelationSquareRoot is:- "
+ accelationSquareRoot);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 5) //
{
bool1 = false;
if (bool == false) {
if (actualTime - lastUpdate < 1000) {
return;
}
lastUpdate = actualTime;
//Here Define Method for OnStartShaking
bool = true;
} else {
return;
}
} else {
bool = false;
System.out.println("Jay Not Stuffing");
if (bool1 == false) {
if (actualTime - lastUpdate < 4000) {
return;
}
lastUpdate = actualTime;
//Here Define Method for OnStopShaking
bool1 = true;
} else {
return;
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
Now Run Your Project.
Enjoy :-)
Declare below Variables or Make Objects of Below Class;
private SensorManager sensorManager;
private long lastUpdate;
Boolean bool = false, bool1 = false;
Write This three Line into Your onCreate Method:-
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this,sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
lastUpdate = System.currentTimeMillis();
Add Following Methods into our Activity:-
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = event.values;
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelationSquareRoot = (x * x + y * y + z * z)
/ (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
System.out.println("accelationSquareRoot is:- "
+ accelationSquareRoot);
long actualTime = System.currentTimeMillis();
if (accelationSquareRoot >= 5) //
{
bool1 = false;
if (bool == false) {
if (actualTime - lastUpdate < 1000) {
return;
}
lastUpdate = actualTime;
//Here Define Method for OnStartShaking
bool = true;
} else {
return;
}
} else {
bool = false;
System.out.println("Jay Not Stuffing");
if (bool1 == false) {
if (actualTime - lastUpdate < 4000) {
return;
}
lastUpdate = actualTime;
//Here Define Method for OnStopShaking
bool1 = true;
} else {
return;
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
Now Run Your Project.
Enjoy :-)
Don’t forget to provide feedback or follow this blog, if you find this blog is useful.
why do i get error at 'void' on 1st method?
ReplyDelete