【问题标题】:Get accelerometer information (x y z coordinates) in android without access to events在不访问事件的情况下在android中获取加速度计信息(x y z坐标)
【发布时间】:2014-08-14 08:35:28
【问题描述】:

我想像以下代码一样获取 android 的加速度计(x y z 坐标):

http://www.techrepublic.com/blog/software-engineer/a-quick-tutorial-on-coding-androids-accelerometer/ (函数:onSensorChanged(SensorEvent事件))

但我无法从代码中捕获事件,因为我不在活动类中。

有没有人知道如何在不访问事件的情况下访问此信息?

【问题讨论】:

  • 具有组合到 Context 对象的常规类。我将 Context 对象作为参数。

标签: android android-activity accelerometer android-sensors android-event


【解决方案1】:

在MainActivity中创建一个全局变量 创建一个具有其他类类型的对象,例如“加速度计”

public static SensorManager sensor_manager = null;
otherclass accelerometer = new otherclass();

在 MainActivity 的 oncreate 中获取传感器服务

sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer.function();

在你的其他类中创建一个函数

function()
{
SensorManager sensor_manager = MainActivity.sensor_manager;
sensor_manager.registerListener(this,
            sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
}

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多