简体   繁体   中英

WakeLock.acquire() does not work

I'm experiencing a problem with a wakelock. In a class, that extends Application class in onCreate() I set an AlarmManager

AlarmManager _alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        _wakeLockIntent = new Intent(getApplicationContext(), RepeatingWakelock.class);
        _wakeLockIntent.setAction(WAKE_LOCK_ACTION);
        _pIntent = PendingIntent.getBroadcast(getApplicationContext(), 26, _wakeLockIntent, 0);
        _alarm.setRepeating(AlarmManager.RTC_WAKEUP, DateUtil.now().getTime(), 60000, _pIntent);

Then, in BroadcastRecevier I try to acquire wakelock, but it does not work.

public static class RepeatingWakelock extends BroadcastReceiver{
    private PowerManager _pm;
    private PowerManager.WakeLock _wl;
        public RepeatingWakelock() {
            super();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (WAKE_LOCK_ACTION.equals(intent.getAction())){
            _pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            _wl = _pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "GPS WAKELOCK, ZOMBIES NEVER DIE!");
                if (!_wl.isHeld()){
                    _wl.acquire();
                }
            }

        }       
    }

Screen just does not turn on. I debugged this and can say for sure that the line _wl.acquire() is invoked, but nothing happens. I have WAKE_LOCK permissions in Manifest file, also I have the broadcast receiver registered there correctly

here they are

<receiver android:name="ru.cdc.android.test.app.Test$RepeatingWakelock">
        <intent-filter>
            <action android:name="wakelock"></action>
        </intent-filter>
    </receiver>


  <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

Any help is much appreciated!

The solution is as simple as 2x2. I just had to add

_wl = _pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP, "GPS WAKELOCK, ZOMBIES NEVER DIE!");`

the "PowerManager.ACQUIRE_CAUSES_WAKEUP" line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM