2

I'm having a really weird issue here. (Running on device,Motorola XT910 Android 4.1.2, Xamarin.Android 4.16.0)

I have an activity with a button that the user presses an holds to record some audio. I handle the onTouch event and listen to the DOWN and UP events (on DOWN I start recording, on UP I stop it). So far so good. Now, I also handle the onSensorChanged event in order to turn the screen off when the user holds up the device to his ear. I achieve this by using a wakelock like this:

private void AcquireScreenWakeLock()
    {
        if (screenWakeLock == null) {
            PowerManager pm = (PowerManager)GetSystemService (Context.PowerService);
            field = pm.Class.GetField ("PROXIMITY_SCREEN_OFF_WAKE_LOCK").GetInt (null);
            screenWakeLock = pm.NewWakeLock ((WakeLockFlags)field, "ScreenWakeLock");
        }
        screenWakeLock.Acquire();
    }

private void ReleaseScreenWakeLock()
    {
        if (screenWakeLock == null)
            return;
        if (screenWakeLock.IsHeld)
            screenWakeLock.Release();
    }

The problem comes when pressing and holding the button to record, right after the DOWN event, I receive an UP event WITHOUT lifting my finger... Of course there are other things that I do in between but I managed to narrow the problem down to this code (if I comment it out everything works as expected).

I know I'm using a hidden field to make the wakeLock (actually it seems it's released in API 21, docs here) but I haven't found a better way to do this, and the whole screen off/on process works flawlessly. I'm having this issue while testing on a Motorola XT910 running Android 4.1.2, I've tested it on a Moto G running Android 4.4.4 and everything works fine there (no UP events being fired up when holding down the button).

I'm really at a loss here figuring this out...how can a wakelock cause this behaviour? Do you have any other way to effectively turn the screen off/on? I've searched other posts but none seemed to help me. Using a partial wakeLock doesn't work either (I've checked with this code). Thanks in advance!

P.S: I've checked and the touch event is only being registered once.

Community
  • 1
  • 1
guitarger
  • 31
  • 1
  • 6
  • is it something to do with the activity going into the `onPause` state when the screen goes off? – behelit Jun 23 '16 at 04:57

1 Answers1

0

PROXIMITY_SCREEN_OFF_WAKE_LOCK turns screen off/on according to proximity state by itself. No need to handle also onSensorChanged event. This might cause an issue.

Igor Gorjanc
  • 535
  • 4
  • 17