我把加速度计代码投入使用。但是当屏幕关闭时它会停止。 通过usb插入电话时它不会停止,但是在未插入电话时它会停止。 所有帖子都说服务中的加速度计不会停止。但是我的代码中的加速度计即使在服务中也会停止。这是我的代码。我需要在代码中添加什么? (我的测试手机是三星Galaxy S3 3G。)
// Service
public class LocalService extends Service {
private final IBinder mBinder = new LocalBinder();
private final Random mGenerator = new Random();
private SensorManager senSensorManager;
private Sensor senAccelerometer;
@Override
public void onCreate() {
super.onCreate();
Log.i("t", "onCreate");
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(mSensorEventListener, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onDestroy() {
super.onDestroy();
senSensorManager.unregisterListener(mSensorEventListener);
// this sound is for checking
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
private SensorEventListener mSensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
Sensor mySensor = event.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// this vibrator is for checking
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(100);
//Log.i("t", "onSensorChanged");
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public int getRandomNumber() {
return mGenerator.nextInt(100);
}
}
// Activity
public class MainActivity extends ActionBarActivity {
LocalService mService;
boolean mBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mBound) {
int num = mService.getRandomNumber();
Toast.makeText(getBaseContext(), "number: " + num, Toast.LENGTH_SHORT).show();
}
}
});
Intent intent = new Intent(this, LocalService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
//添加了 我决定使用PARTIAL_WAKE_LOCK。这对我来说是最好的解决方案。 读这个。 http://nosemaj.org/android-persistent-sensors http://nosemaj.org/android-persistent-sensors
答案 0 :(得分:0)
在您的代码中,您使用LocalService
绑定加入Activity
(您正在调用bindService
和{{ 1}})。这样做会导致您服务停止然后停止活动,因为您的服务充当bound service并在最后一次绑定关闭时停止。 See bound service lifecycle for reference
如果你真的希望你的服务一直运行不要绑定它 - 你应该只是在某个地方开始(使用startService
方法),然后停止那时你真的不再需要加速度计跟踪。
修改强>
如果问题不是服务停止,而是加速计停止,那么您应该maintain the wakelock,否则设备将关闭,传感器将不会向您报告任何数据。