Skip to main content

Using Accelerometer when Screen Off


Introduction

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;
     
       @Override
       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);
             filter.addAction(Intent.ACTION_SCREEN_OFF);
             mReceiver = new ScreenReceiver();
             registerReceiver(mReceiver, filter);
             super.onCreate();
       }    

       @Override
       public void onDestroy() {
// Unregisters sensorEventListenerImpl. This will stop notifications for all sensors
             mySensorManager.unregisterListener(this);
             unregisterReceiver(mReceiver);
             super.onDestroy();
       }

      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     
      }

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

public class ScreenReceiver extends BroadcastReceiver {

             @Override
             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.unregisterListener(this);
             mySensorManager.registerListener(thismySensors.get(0), SensorManager.SENSOR_DELAY_NORMAL);
       }



Conclusion


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.

Comments

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

    ReplyDelete
  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.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to change position of MyLocationButton on new Map API v2 - 2

In my previuos post[1], i mention a workaround for changing buttons on Google Map which we use with google services. It is good to know they listen developers and make their products more development friendly. (I hope it goes like that way all the time and all the product lines of Google) Now we have a method for setting padding of map. It is add padding to useful area of map. We can see full screen map but with padding we can use prefered area of it. It is good for overlay action bar and other things on map view. [ 2] They write a blog and take a video about it. Read blog and watch video if you are using google map in your app :) [3] And finally, They add this method because of this issue. That means, if you find a bug or want extra feature from and api, write to google via this issue tracking system. [1] http://blog.kozaxinan.com/2013/08/how-to-change-position-of.html [2] https://developers.google.com/maps/documentation/android/map#map_padding [3] http://googlegeodeve

How to set alarm to show a notification at future date with AlarmManager?

Lets assume you want to do a job a future time or show a notification at a selected time. AlarmManager can help to send an intent in future time. First we need to select date, Lets create a date in 2050, Calendar calendar = Calendar. getInstance () ; calendar.set(Calendar. YEAR , 2050 ) ; Date date = calendar.getTime() ; For showing notification we need a service. public class AlarmReceiver extends Service { public AlarmReceiver() { } @Override public IBinder onBind (Intent intent) { } } Add service to Manifest.xml <service android :name= "com.kozaxinan.example.alarm.AlarmReceiver" android :enabled= "true" android :exported= "true" > </service> We will modify AlarmReceiver for sending notification. But first we need to register a alarm. AlarmManager alarmManager = (AlarmManager) getSystemService( ALARM_SERVICE ) ; Intent intent = new Intent( this, AlarmReceiver. class ) ; intent.putExtra( &q