Skip to main content

Using Accelerometer when Screen Off


Android Phones have a lot of built-in sensor such as gyro, proximity and accelerometer.

Using Accelerometer in Android is a known thing. For listening sensors, class or service needs a SensorEventListener implementation. An Activity is only exists when you are seeing it. When phone enters standby mode, activity which was recently active frozen and restored whenever needed. While this frozen time, activity doesn’t run. Because of that it doesn’t need sensor updates. And Android stop to listen some sensors for power management.

Services have this condition like classes (activity). In contrast to class, a service run every time, even when phone is in standby mode. Therefore Services needs sensor updates for some situations. But android stop to listen some sensor(accelerometer) in some different phones.

There is a solution for that. This is not an exact solution but it work with a lot of phones.

With a BroadcastReceiver listen Intent.ACTION_SCREEN_OFF and register sensor listener again after screen goes off.

public class YonService extends Service implements SensorEventListener {
       private static SensorManager mySensorManager;
       BroadcastReceiver mReceiver;
       Context context;
       List mySensors;
       public void onCreate() {
             mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
             mySensors = mySensorManager.getSensorList(Sensor.TYPE_ORIENTATION);

             if(mySensors.size() > 0){
                    mySensorManager.registerListener(thismySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
             context = YonService.this.getApplicationContext();

             IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
             mReceiver = new ScreenReceiver();
             registerReceiver(mReceiver, filter);

       public void onDestroy() {
// Unregisters sensorEventListenerImpl. This will stop notifications for all sensors

      public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub

      public void onSensorChanged(SensorEvent event) {
            float azimuth = Math.round((float)event.values[0]);
            float pitch = Math.round((float)event.values[1]);
            float roll = Math.round((float)event.values[2]);
            Log.e("Values", azimuth +" "+pitch+" "+roll)
// Do anything you want with values     

      public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;

public class ScreenReceiver extends BroadcastReceiver {

             public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// Unregister and register listener after screen goes off.
                          refreshListener ();

       public void refreshListener(){
             mySensorManager.registerListener(thismySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);


I have test this workaround with my Samsung Galaxy S2 and a Galaxy mini. This code works on S2. But on Galaxy mini, listener register but after screen off phone give result for accelerometer but not real result; just it repeats last result when screen goes off. This code doesn't work on every phone.

Listening sensor in standby mode uses more battery. This is a problem for user, you should warn the user about battery usage.

If you find my blog or apps usefull, please consider donation.


  1. Hello. Please support my Note 2 samsung. It doesn't work when the screen is off. Please help. Thank you

  2. @John I think the workaround is clear. It is just a method for using accelerometer when screen off. But it is not solution for all phone.


Post a Comment

Popular posts from this blog

Eclipse'te Android Sanal Makinesi oluşturma

Eclipse'te oluşturduğumuz projeleri denemek için Eclipse'te Android Sanal makinesı oluşturabiliriz. Bu yaptığımız programları önce bilgisayar üzerinde denememiz ve ilk izlenimlerimizi oluşturmamız için önemlidir. Her android sürümü için farklı sanal makine oluşturabileceğimiz için yazdığımız programın farklı sürümlerde nasıl çalıştığını görmemize yardımcı olur. Bu sanal makineyi önceki yazılarda anlattığımız şekilde kurduğumuz Eclipse'te  bir kaç adımda oluşturabiliriz. Bunun içim aşağıdaki adımları izlememiz yeterli olucaktır. Windows-> AVD Manager a tıklayalım. Açılan pencerede sağ üstten "New"e basarak yeni sanal makine oluşturacağımız ekrana gelelim. Burda "Name" kısmına istediğimiz ismi yazabiliriz. Ama tavsiyem oluşturacağınız android sürümü ile alakalı isimler verin. örn: Android 2.1 sürümü için Andro_2.1 şeklinde isim verebilirsiniz. "Target" kısmında oluşturacağınız sanal makinenin API sini seçmelisiniz. SD kart kısmına

How to change position of MyLocationButton on new Map API v2? Explained

When an app use map and design some layout on this map (TextView, Button or image), developer needs to change position of default buttons of map view. (MyLocationButton, ZoomControl or compass...) As far as i know, there are two possible way for changing position of myLocationButton's position. 1- * - Disable it     * - Create new button programmatically     * - Add animateCamera(toCurrentLocation) to onClickListener of this button     * - And place it wherever you want But if you don't want to write all that there is a hack for that. 2 - * - findViewById() of hardcoded button with; ZoomControl id = 0x1 MyLocation button id = 0x2      * - And do whatever you want: // Find map fragment SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(; // Find myLocationButton view View myLocationButton = mapFragment.getView().findViewById(0x2); if (myLocationButton != null && myLocationButton.get