0

I've using alarm manager to call an activity and I'm using the wake locker class onRecive() to wake the phone and then calling WakeLocker.release() after the Activity is over but the screen still stays on...

Receive.class:

public class MyScheduledReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    WakeLocker.acquire(context);

Activity.class

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        WakeLocker.release();
        finish();
        }

I've put it in the onPause(), onStop() everywhere... the thing won't release and the screen won't turn off automatically after my app closes...

Community
  • 1
  • 1
user961389
  • 423
  • 2
  • 8
  • 19

3 Answers3

1

Make sure you request permission

<uses-permission android:name="android.permission.WAKE_LOCK" />
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
0

Try this

PowerManager pm;
PowerManager.WakeLock wakeLock;

pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                    wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
                            "x2_wakelook");


wakeLock.acquire();
wakeLock.release();
Ahmed
  • 814
  • 2
  • 19
  • 38
0

You are starting the wakelock in a broadcastreceiver and stopping it in an activity. You are referencing 2 different instances of a wakelock. You should start the activity from the onreceive and in onresume acquire the wake lock, then still release in the onpause if that is where you want it to happen.

You should never start anything that is supposed to be around for awhile within a broadcastreceiver, because the receiver is destroyed as soon as possible.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • I am starting in the onReceive... right after I acquire the wakelock... `Intent scheduledIntent = new Intent(context, Activity.class); ` scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(scheduledIntent); – user961389 Dec 21 '11 at 20:37
  • This line: `WakeLocker.acquire(context);` should be removed from your `onReceive`, and then added to the `onResume` of the Activity that you launch. With the code you have in your question, you create acquire one `WakeLock` in your `onReceive` then you release a different one in the `onPause`. – Reed Dec 21 '11 at 20:47
  • Doesn't work... I should be ablke to acquire the WL in the Broadcast Receiver to wake the phone and release the WL when the activity the Broadcast Receiver starts is over. – user961389 Dec 21 '11 at 21:46
  • It is doable with a static wakelock if you manage it correctly. Could you edit your question to show how you create the WakeLock in both your onReceive and in your Activity before you release? – Reed Dec 21 '11 at 22:05