如何从单独的类中读取传感器的数据

时间:2014-11-17 19:29:58

标签: java android sensor android-sensors

我的代码有问题。我有两个类:MainActivity,我检查传感器(在这种情况下是光传感器)是否可用,如果是 - 我尝试从另一个类LightSensor获取传感器的日期,但结果始终为null。我认为我对听众做错了但我不知道是什么......我坐了几个小时但仍然没有......如果你有任何想法,请写信给我帮助。

MainActivity类:

`public class MainActivity extends Activity implements EventListener {

    SensorManager mSensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView wyswietl = (TextView)findViewById(R.id.res);

        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        LightSensor mLightSensor = new LightSensor(getBaseContext());

        if (mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT) != null){
            //mLightSensor.register();
            String newLux = mLightSensor.getLux();
            wyswietl.setText("Light level: " + newLux);
        }
        else{

        }
}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }`

在MainActivioty类中,我不知道构造函数中的参数:     LightSensor mLightSensor = new LightSensor(getBaseContext());很好......

LightSensor类:

`public class LightSensor implements SensorEventListener {

    public SensorManager mSensorManagerx;
    public Sensor lightManager;
    public String lux;
    Context context;

    public LightSensor(Context context){
    //public void onCreateLight(Context context){
        this.context = context;
        mSensorManagerx = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        lightManager = mSensorManagerx.getDefaultSensor(Sensor.TYPE_LIGHT); 
    }

    public void register(){
        mSensorManagerx.registerListener(this, lightManager, SensorManager.SENSOR_DELAY_NORMAL);
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        lux = Float.toString(event.values[0]);  
    }
    public String getLux(){
        return lux;
    }

    public void unregister(){
        mSensorManagerx.unregisterListener(this);
    }

}`

2 个答案:

答案 0 :(得分:1)

您不应该使用getter来实现此目的,因为在未知时间之后,您的获取值将被初始化。因此,当您调用getLux方法时,它仍然可以为null。

您应该使用侦听器模式。我已经更改了一些代码,为您提供示例实现。

<强> LightSensor:

public class LightSensor implements SensorEventListener {

    public static interface LightSensorListener {
        abstract void onLightSensorChanged(String lux);
    }

    private LightSensorListener listener;

    private SensorManager mSensorManagerx;
    private Sensor lightManager;

    public LightSensor(Context context) {
        mSensorManagerx = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        lightManager = mSensorManagerx.getDefaultSensor(Sensor.TYPE_LIGHT);
    }

    public void setListener(LightSensorListener listener) {
        this.listener = listener;
    }

    public boolean register() {
        return mSensorManagerx.registerListener(this, lightManager, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (listener != null) {
            listener.onLightSensorChanged(Float.toString(event.values[0]));
        }
    }

    public void unregister() {
        mSensorManagerx.unregisterListener(this);
    }
}

<强>活动:

public class ActivityLightSensor extends Activity implements LightSensorListener {

    private TextView text;
    private LightSensor mLightSensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView)findViewById(R.id.test);

        mLightSensor = new LightSensor(getBaseContext());
        mLightSensor.setListener(this);
    }

    @Override
    public void onLightSensorChanged(String lux){
        text.setText("Light level: " + lux);
    }

    @Override
    protected void onResume() {
        super.onStart();
        if(!mLightSensor.register()){
            Toast.makeText(this, "Light sensor not supported!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onPause() {
        super.onStop();
        mLightSensor.unregister();
    }
}

答案 1 :(得分:0)

在创建侦听器后,不会调用

onSensorChanged事件。它会在某些东西改变传感器值后被调用,然后它会被调用。所以你应该实现一些回调,或者,对我来说,更好的方法是在你的活动中实现SensorEventListener,然后只在onSensorChanged事件方法调用中

wyswietl.setText("Light level: " + newLux);