質問

I have a LightSensor which broadcasts changes. Basically it is broadcasting on every sampling even if there is no change from 0. How should I get the Sensor to only broadcast basically an ON/OFF value. For example if lux > 0 then light is ON else lux = 0 so light is OFF.

sendLuxUpdate():

public class LightSensor extends Service implements SensorEventListener {

    private SensorManager mSensorManager;
    public Sensor LightSensor;
    public static Float lightLux;
    TextView tvLightSensorLux;
    public String Lux;

    public void onCreate() {

        Log.d("LightSensor", "OnCreate");
        // Get an instance of the sensor service
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor LightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

        // Test to see if Light Sensor is available
        if (LightSensor != null) {
            mSensorManager.registerListener(this, LightSensor,
                    SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    public void onStartCommand() {
        Log.d("LightSensor", "OnStartCommand");
    }

    /**
     * protected void onResume() { mSensorManager.registerListener(this,
     * LightSensor, SensorManager.SENSOR_DELAY_NORMAL); // super.onResume(); }
     * 
     * protected void onPause() { mSensorManager.unregisterListener(this,
     * LightSensor); // super.onPause(); }
     **/
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onSensorChanged(SensorEvent event) {
        lightLux = event.values[0]; // Final output of this sensor.
        Lux = String.valueOf(lightLux);

        if (lightLux > 0) {
            Log.d("LightSensor", Lux);
            sendLuxUpdate();
        } else {
            float lightLux = 0;
            sendLuxUpdate();
        }

    }

    private void sendLuxUpdate() {
        Log.d("sender", "Broadcasting message " + Lux);
        Intent intent = new Intent("LuxUpdate");
        intent.putExtra("Lux", Lux);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onDestroy() {
        stopSelf();
        mSensorManager.unregisterListener(this, LightSensor);
    }

}
役に立ちましたか?

解決

Problem is that Sensors send you raw analog data. So they are continuous and data is raw. Or if you are using other sensors too, this method might be called by other sensors. So check type of sensor also.

public void onSensorChanged(SensorEvent event) {
        lightLux = event.values[0]; // Final output of this sensor.
        Lux = String.valueOf(lightLux);

        if(event.sensor.getType()==Sensor.TYPE_LIGHT){
        final float currentReading = event.values[0];
        if (currentReading > 0){
        //ON
          }
        else
         {//OFF     
           }
        }

    }

Also you will see change in value everytime, because it sends too detailed value as it is raw data. You can cast it to int if you don't want this.

int value= (int) event.values[0];

Hope it helps! Good luck

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top