1

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

Jeonggu
  • 11
  • 4
  • 1
    possible duplicate of [Android accelerometer not working when screen is turned off](http://stackoverflow.com/questions/9982433/android-accelerometer-not-working-when-screen-is-turned-off) – Randy Dec 12 '14 at 02:48

1 Answers1

0

In your code you're using LocalService, which is bound to the Activity (you're calling bindService and unbindService). Doing so causes you service to stop then activity stops, because your service acts as a bound service and stops, whenever last binding is closed. See bound service lifecycle for reference.

If you really want your service to run always don't bind to it - you should just start is somewhere (using startService method), and then stop only then you really no more needed accelerometer tracking.

EDIT

If the problem is not that the service is stopping, but rather that the accelerometer stops, then you should maintain the wakelock, otherwise the device is turning off and the sensors will not report you any data.

dant3
  • 966
  • 9
  • 26
  • I modified my question. Accelerometer stops when screen is off, not when activity gets in the pause mode. Service does not stop. Just accelerometer stops. I added some codes to play sound when my service is destoryed for cheking it. but my app doesn't play this sound when accelemeter stops. It means service doesn't stop and only accelerometer stops. I don't know why acceleromet in service stops when screen gets off. – Jeonggu Dec 11 '14 at 23:57
  • Then it looks like you need to maintain wakelock. – dant3 Dec 12 '14 at 12:20
  • I corrected my answer to mention this. It wasn't clear for me which one stops for you - the accelerometer or the service itself. – dant3 Dec 15 '14 at 08:29