I put accelerometer code in service. But it stops when screen is turned off. It doesn't stop when phone is plugged via usb but it stops when not plugged. All posts say that accelerometer in service doesn't stop. But the accelerometer in my code stops even though it is in service. Here's my code. what do I have to add in my code? (My test phone is Samsung 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;
}
};
// added
I've decided to use PARTIAL_WAKE_LOCK. It's the best solution for me.
Read this.
http://nosemaj.org/android-persistent-sensors
http://nosemaj.org/android-persistent-sensors