当屏幕关闭时,压力传感器停止提供样品

时间:2013-11-08 16:20:59

标签: android screen sensor wakelock suspend

这是我在这里的第一篇文章。在提出我的问题之前,我想感谢本网站的团队和所有贡献者。这里的信息真的帮助我开始为Android开发。 这是我的问题:我为DashClock编写了一个小扩展,显示了根据压力传感器数据计算出的当前高度。只要屏幕打开,一切正常。但是当温度关闭时,压力传感器会停止传送样品。我尝试了很多,但没有成功:设置唤醒锁,注销并重新注册SensorEventListener,将其放入指定的线程中...是否有人知道如何使其工作?我每秒都不需要样品,每分钟左右就可以了。以下是我的代码的相关部分:(RTC_WAKEUP - > AlarmReceiver.onReceive - >启动SensorService)

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Alarm!");
        context.startService(new Intent(context, PressureSensorService.class));
    }
}

public class PressureSensorService extends Service implements SensorEventListener {

    final static String SENSOR_UPDATE_ACTION = "SENSOR_UPDATE_ACTION";
    private float pressure;
    private SensorManager mgr;
    private Sensor pressuresensor;
    Intent intent = new Intent("SENSOR_UPDATE_ACTION");
    @Override
    public void onCreate(){
        System.out.println("PressureSensorService created!");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("PressureSensorService started!");
        mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        pressuresensor = mgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
        mgr.registerListener(this, pressuresensor, SensorManager.SENSOR_DELAY_NORMAL);
        return START_STICKY;
    }
    @Override
    public void onDestroy(){
        mgr.unregisterListener(this, pressuresensor);
        System.out.println("PressureSensorService stopped!");
    }
    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        PressureSensorService.this.pressure = event.values[0];
        intent.putExtra("pressure", pressure);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        System.out.println("Pressure = "+ pressure + " hPa");
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

0 个答案:

没有答案