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 {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
public void onSensorChanged(SensorEvent event) {
@Override
public class ScreenReceiver extends BroadcastReceiver {
@Override
public void refreshListener(){
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.
If you find my blog or apps usefull, please consider donation.
Hello. Please support my Note 2 samsung. It doesn't work when the screen is off. Please help. Thank you
ReplyDelete@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