Java LibGDX-在运行时激活加速计
我有一个应用程序使用加速度计,但只是偶尔在极少数情况下使用。我想通过在默认情况下禁用电池并在需要时打开来保护电池 我发现的唯一一件事是在从初始化应用程序时设置配置 但我找不到在应用程序运行时启用/禁用它的方法。有人有主意吗 编辑: 在上面的示例中,AndroidPlatform正在核心项目中实现平台接口。我尝试了Zoe的想法,将配置传递给平台实现,并对其进行如下更改:Java LibGDX-在运行时激活加速计,java,android,libgdx,Java,Android,Libgdx,我有一个应用程序使用加速度计,但只是偶尔在极少数情况下使用。我想通过在默认情况下禁用电池并在需要时打开来保护电池 我发现的唯一一件事是在从初始化应用程序时设置配置 但我找不到在应用程序运行时启用/禁用它的方法。有人有主意吗 编辑: 在上面的示例中,AndroidPlatform正在核心项目中实现平台接口。我尝试了Zoe的想法,将配置传递给平台实现,并对其进行如下更改: @Override public void enableAccelerometer(boolean enable) {
@Override
public void enableAccelerometer(boolean enable) {
config.useCompass = enable;
config.useAccelerometer = enable;
}
然后在核心项目中,当应启用加速计时:
private void startInclineMonitoring() {
System.out.println("Before:");
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer));
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Compass));
platform.enableAccelerometer(true);
System.out.println("After:");
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer));
System.out.println(Gdx.input.isPeripheralAvailable(Input.Peripheral.Compass));
}
不幸的是,这导致:
I/System.out: Before:
I/System.out: false
I/System.out: false
I/System.out: After:
I/System.out: false
I/System.out: false
所以,这里没有运气。到今天为止,似乎没有简单的方法来实现这一点,但我最终完成了自己的(Android)运动传感器实现。我想我会把它分享给未来的访客: 这假设您拥有平台接口和特定于平台的实现,如中所述 首先,将这些方法添加到接口:
public interface Platform {
public void startMotionSensors(PlatformCallback<float[]> callback);
public void stopMotionSensors();
}
公共接口平台{
公共无效启动运动传感器(平台回调);
公共空间传感器();
}
在android实现中:
public class AndroidPlatform implements Platform {
private Activity activity;
private MotionSensor motionSensor;
private Handler handler;
public AndroidPlatform(Activity activity) {
this.activity = activity;
this.motionSensor = new MotionSensor(activity);
this.handler = new Handler();
}
@Override
public void startMotionSensors(final PlatformCallback<float[]> callback) {
handler.post(new Runnable() {
@Override
public void run() {
motionSensor.start(callback);
}
});
}
@Override
public void stopMotionSensors() {
handler.post(new Runnable() {
@Override
public void run() {
motionSensor.stop();
}
});
}
}
公共类AndroidPlatform实现平台{
私人活动;
专用运动传感器;
私人经办人;
公共AndroidPlatform(活动){
这个。活动=活动;
this.motionSensor=新的motionSensor(活动);
this.handler=new handler();
}
@凌驾
public void startMotionSensors(最终平台回调){
handler.post(新的Runnable(){
@凌驾
公开募捐{
motionSensor.start(回调);
}
});
}
@凌驾
公共空间传感器(){
handler.post(新的Runnable(){
@凌驾
公开募捐{
运动传感器。停止();
}
});
}
}
MotionSensor类别:
public class MotionSensor implements SensorEventListener {
private Activity activity;
private SensorManager sensorManager;
private float[] gravity = new float[3];
private float[] geomag = new float[3];
private float[] rotationMatrix = new float[16];
private float[] inclinationMatrix = new float[16];
private PlatformCallback<float[]> callback;
public MotionSensor(Activity activity) {
this.activity = activity;
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
gravity = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomag = event.values.clone();
break;
}
if (gravity != null && geomag != null) {
boolean success = SensorManager.getRotationMatrix(rotationMatrix,
inclinationMatrix, gravity, geomag);
if (success) {
notifyCallback(new Result(), rotationMatrix);
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
private void notifyCallback(Result result, float[] rotationMatrix) {
callback.callback(result, rotationMatrix);
}
public void start(PlatformCallback<float[]> callback) {
this.callback = callback;
sensorManager = (SensorManager) activity.getSystemService(Activity.SENSOR_SERVICE);
if (sensorManager != null) {
boolean accelerometerSupport = sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
boolean magneticFieldSupport = sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
SensorManager.SENSOR_DELAY_UI);
if (!accelerometerSupport || !magneticFieldSupport) {
sensorManager.unregisterListener(this);
notifyCallback(new Result(Result.STATE.FAILED, "Not supported"), null);
}
} else {
notifyCallback(new Result(Result.STATE.FAILED, "Not supported"), null);
}
}
public void stop() {
if (sensorManager != null) {
sensorManager.unregisterListener(this);
}
}
}
公共类MotionSensor实现SensorEventListener{
私人活动;
私人传感器管理器传感器管理器;
私有浮动[]重力=新浮动[3];
私有浮动[]geomag=新浮动[3];
私有浮点[]旋转矩阵=新浮点[16];
私有浮动[]倾斜矩阵=新浮动[16];
私有平台回调;
公众运动传感器(活动){
这个。活动=活动;
}
@凌驾
传感器更改时的公共无效(传感器事件){
开关(event.sensor.getType()){
外壳传感器.U型加速计:
重力=event.values.clone();
打破
外壳传感器。类型\u磁场:
geomag=event.values.clone();
打破
}
if(重力!=null&&geomag!=null){
布尔成功=SensorManager.getRotationMatrix(rotationMatrix,
倾斜矩阵、重力、几何图形);
如果(成功){
notifyCallback(新结果(),旋转矩阵);
}
}
}
@凌驾
精度更改时的公共无效(传感器,int i){
}
私有void notifyCallback(结果,float[]旋转矩阵){
回调(结果,旋转矩阵);
}
公共无效启动(平台回调){
this.callback=回调;
sensorManager=(sensorManager)activity.getSystemService(activity.SENSOR\u SERVICE);
if(传感器管理器!=null){
boolean ACCELEROMETER Support=sensorManager.registerListener(这是sensorManager.getDefaultSensor(Sensor.TYPE_Accelerator)),
SensorManager.SENSOR\u DELAY\u UI);
布尔值magneticFieldSupport=sensorManager.registerListener(这是sensorManager.getDefaultSensor(Sensor.TYPE\u MAGNETIC\u FIELD),
SensorManager.SENSOR\u DELAY\u UI);
如果(!加速计支架| |!磁场支架){
sensorManager.UnregistereListener(此);
notifyCallback(新结果(Result.STATE.FAILED,“不支持”),null;
}
}否则{
notifyCallback(新结果(Result.STATE.FAILED,“不支持”),null;
}
}
公共停车场(){
if(传感器管理器!=null){
sensorManager.UnregistereListener(此);
}
}
}
以及PlaformCallback类:
public abstract class PlatformCallback<T> {
public void callback(final Result result, final T t) {
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
doCallback(result, t);
}
});
}
protected abstract void doCallback(Result result, T t);
}
公共抽象类平台回调{
公共无效回调(最终结果,最终T){
Gdx.app.postRunnable(新的Runnable(){
@凌驾
公开募捐{
doCallback(结果,t);
}
});
}
受保护的摘要无效文件回拨(结果T);
}
在核心项目中,您现在只需打开和关闭运动传感器即可:
private void startMotionSensor() {
platform.startMotionSensors(new PlatformCallback<float[]>() {
@Override
protected void doCallback(Result result, float[] rotationMatrix) {
if (result.ok()) {
// Do what you want with the rotation matrix
}
}
});
}
public void stopMotionSensor() {
platform.stopMotionSensors();
}
private void startMotionSensor(){
platform.startMotionSensors(新平台回调(){
@凌驾
受保护的void doCallback(结果,float[]旋转矩阵){
if(result.ok()){
//对旋转矩阵执行所需操作
}
}
});
}
公共空间停止运动传感器(){
平台。停止运动传感器();
}
您可以将其作为参数传递给构造函数,并尝试通过实例启用它,但我不确定它是否有效您的意思是将配置作为参数传递给应用程序构造函数?可以把它交给核心项目。如果AndroidConfig类不可用,请在android项目中创建一个包装类,在核心项目中创建一个接口或抽象类(请参见上面的编辑),但没有成功
private void startMotionSensor() {
platform.startMotionSensors(new PlatformCallback<float[]>() {
@Override
protected void doCallback(Result result, float[] rotationMatrix) {
if (result.ok()) {
// Do what you want with the rotation matrix
}
}
});
}
public void stopMotionSensor() {
platform.stopMotionSensors();
}