0

I've a broadcast receiver for phone state changes, and when phone state is "OFFHOOK", I register an eventlistener for proximity sensor. When the proximity changes , I do something(I do the registering and the 'something' in a service ,for that matters). So far everything works as expected. The problem is that screen is not getting locked when the proximity value is 'CLOSE'. I think this is because my listener is catching the proximity change event and so the phone's default application ain't getting the event. Is there a way to pass on the event, like 'return false' in onClickListener() of buttons.

edit:

Codes

In the oncreate of the service

        myProximitySensor = mySensorManager
                .getDefaultSensor(Sensor.TYPE_PROXIMITY);
        mySensorManager.registerListener(this, myProximitySensor,
            SensorManager.SENSOR_DELAY_NORMAL);

I implemented the SensorEventListener, and in it's onSensorChanged() method:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audioManager.setSpeakerphoneOn(true);
Savin
  • 51
  • 6
  • can you please post some code? – Ridcully Oct 17 '12 at 05:32
  • Contrary to what you assume the proximity event does NOT get consumed. All the eventListeners registered will receive the event. So there is no need to "pass-on". Maybe the app/service is holding onto a wakelock that keeps/pokes the device screen ON. Hmmm?... – TheCodeArtist Oct 17 '12 at 06:26
  • @TheCodeArtist Thanks for the reply.Do you think calling a service from the broadcast receiver is causing the problem? – Savin Oct 17 '12 at 07:28
  • Check http://developer.android.com/reference/android/os/PowerManager.html for more info. Is there any `wakelock` being acquired/released in broadcast-receiver or the service in question?... – TheCodeArtist Oct 17 '12 at 07:57
  • no wakelock is aquired or released.The problem is spotted ,thanks to you.I am using AudioManager to enable speakerphone when phone is held away from user.When I commented the AudioManager code, the screen locking is working.Is there a way I can turn on the speakerphone without this 'screen not locking' effect ? – Savin Oct 17 '12 at 08:01
  • to be precise "audioManager.setSpeakerphoneOn(state);" code is causing the trouble. – Savin Oct 17 '12 at 08:05

1 Answers1

0

If you want to lock your screen when proximity listener change you can have an instance of PowerManager.WakeLock and call acquire on it to lock sceen and then call release to unlock the screen again

  • Welcome to Stack Overflow. Please review [How do I write a good answer](https://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question). – Rick Feb 13 '18 at 15:45