mirror of
https://github.com/apache/cordova-android.git
synced 2025-03-06 18:21:13 +08:00
Preliminary Accelerometer Support for Android!
This commit is contained in:
parent
b50f0c99e6
commit
9574e509e2
48
src/com/nitobi/phonegap/Orientation.java
Normal file
48
src/com/nitobi/phonegap/Orientation.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package com.nitobi.phonegap;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
import android.hardware.SensorListener;
|
||||||
|
import android.webkit.WebView;
|
||||||
|
|
||||||
|
public class Orientation implements SensorListener{
|
||||||
|
|
||||||
|
private WebView mAppView;
|
||||||
|
private SensorManager sensorManager;
|
||||||
|
private Context mCtx;
|
||||||
|
|
||||||
|
Orientation(WebView kit, Context ctx) {
|
||||||
|
mAppView = kit;
|
||||||
|
mCtx = ctx;
|
||||||
|
sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||||
|
this.resumeAccel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onSensorChanged(int sensor, final float[] values) {
|
||||||
|
if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3)
|
||||||
|
return;
|
||||||
|
float x = values[0];
|
||||||
|
float y = values[1];
|
||||||
|
float z = values[2];
|
||||||
|
mAppView.loadUrl("javascript:gotAcceleration(" + x + ", " + y + "," + z + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onAccuracyChanged(int arg0, int arg1) {
|
||||||
|
// This is a stub method.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pauseAccel()
|
||||||
|
{
|
||||||
|
sensorManager.unregisterListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resumeAccel()
|
||||||
|
{
|
||||||
|
sensorManager.registerListener(this,
|
||||||
|
SensorManager.SENSOR_ACCELEROMETER,
|
||||||
|
SensorManager.SENSOR_DELAY_GAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user