0

I'm tryng to turn proximitywakelock on when portrait and off when landscape using this code, but it always stays active:

//in onCreate() of my Activity

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mProximityWakeLock = pm.newWakeLock(32, ""); // proximity_wake_lock=32
    mProximityWakeLock.setReferenceCounted(false);

    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "non landascape", Toast.LENGTH_SHORT).show();

        if (!mProximityWakeLock.isHeld()) {
            mProximityWakeLock.acquire();
            Toast.makeText(this, "acquired", Toast.LENGTH_SHORT).show();
        }

        if (!mProximityWakeLock.isHeld())
            Toast.makeText(this, "not held", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, " held", Toast.LENGTH_SHORT).show();

    } else {

        if (!mProximityWakeLock.isHeld())
            Toast.makeText(this, "not held", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, " held", Toast.LENGTH_SHORT).show();

        Toast.makeText(this, "landascape", Toast.LENGTH_SHORT).show();

        if (mProximityWakeLock.isHeld()) {
            mProximityWakeLock.release();
            Toast.makeText(this, "released", Toast.LENGTH_SHORT).show();
        }

    }

It looks like it's never held when on landscape... How does isHeld() actually works? What's the problem with the code?

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Angelo Tricarico
  • 1,333
  • 1
  • 19
  • 36

1 Answers1

0

I looks like you are creating a new wakelock every time you rotate the screen and the new wakelock is not held.

If you look in the logs you might see something about leaking wakelocks.

tidbeck
  • 2,363
  • 24
  • 35
  • Why do you say that? The wakelock object creation is outside (before) the orientation check. – Angelo Tricarico Feb 17 '12 at 13:45
  • @Deleted I'm saying that you create a new wakeLock every time the screen is rotated. But are you ever releasing the old one? E.g. in `onDestroy()` (maybe not the best place since it's not guarantied to be called, but you I hope you understand what I mean). – tidbeck Feb 20 '12 at 09:51