Galaxy Note2:压力传感器停在屏幕上

时间:2013-11-10 12:24:06

标签: java android listener android-sensors pressure

我正在尝试在三星N7100上记录压力数据。 我的问题是在屏幕变暗后不会调用onSensorChanged()方法。在屏幕上一切都开始正常工作。 任何人都能指出我正确的方向吗? 尝试设置一个唤醒锁,在前台运行服务,重新注册听众,....,没有任何作用。请帮忙: - )

//MainActivity starts service (this is just a test, not the real app I am building)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this, sensorservice.class));
}

@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;
}

}

这是服务:

public class sensorservice extends Service implements SensorEventListener{

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Notification noti = new Notification.Builder(this)
    .setContentTitle("test")
    .setContentText("test")
    .setSmallIcon(R.drawable.ic_launcher)
    .setOngoing(true)
    .build();
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    startForeground(5, noti);
    PowerManager pm = (PowerManager)this.getSystemService(sensorservice.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"Tag");
    wl.acquire();
    SensorManager mgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
    Sensor pressuresensor = mgr.getDefaultSensor(Sensor.TYPE_PRESSURE);
    mgr.registerListener(this, pressuresensor, SensorManager.SENSOR_DELAY_NORMAL);
    return START_STICKY;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
    float pressure = event.values[0];
    System.out.println("Pressure = "+ pressure + " hPa");       
}

}

0 个答案:

没有答案