I wrote a simple countdown timer app and I'm using the code below to turn the screen on, vibrate and play an alarm sound whenever a countdown is finished:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Vibrator v = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
RingTone r = RingtoneManager.getRingtone(mContext, notification);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "Countdown Timer");
wl.acquire(1000);
v.vibrate(pattern, -1);
r.play();
Although this works fine when the phone is charging, when I disconnect the charger the wakelock doesn't turn the screen on and I only get a vibration and an alarm when I turn on the screen manually using the power button.
When I tested this on a different device it seems to be working fine.
Any thoughts?
Your code wl.acquire(1000);
will release the wakelock after 1s. Try this code:
wl.acquire();
v.vibrate(pattern, -1);
r.play();
w1.release();
I'm using this which works fine
// set up wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
wakelock.acquire();
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.