I am trying to set up the SensorEventListener for both Service and
MainActivity classes with the same Shake_ThresHold value.
This setup works fine on Galaxy S3 Android 4.1.2 and Galaxy Note 2 Android
4.3.
However, the results are not the same on Galaxy Note 4 Android 5.1.1. On
MainActivity class, I had to shake 1.5-2 times harder than on the Service
Class.
Here is my code:
*MainActivity:*
public class MainActivity extends Activity implements SensorEventListener {
private float Shake_threshold = 40;
private float Last_Z = 0f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mVibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(MainActivity.this, accelerometer,
SensorManager.SENSOR_DELAY_UI);
.......
@Overridepublic void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float Z_axis = event.values[2];
float zChange = Math.abs(Z_axis - Last_Z);
Last_Z = Z_axis;
if(zChange > Shake_threshold {
Last_Z = 0f;
mVibrator.vibrate(100);
}
} }
@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) { }}
*Service Class:*
I have exact the same setup...
I have stared at both classes to see if there was any difference for
SensorEventListener setup, but I could not find anything difference between
the two classes.
I've spent hours trying to figure out what I did wrong..
Can someone please help me on this?
Thank you very much for your help.
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/ff0eff8a-a3ea-410b-acb5-2b3806017bce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.