Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: wake up device using Alarm

I'm using the follwing code for setting Alarm:

Intent intent = new Intent(context, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(reminderContext, 0, intent,
    PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis()+delay,pendingIntent);

When the alarm goes off, I executed same code that show a new Activity and a sound is reproduced. This is good, but if my device is in sleep mode, when the alarm goes off I can hear just the sound. No activity is shown and my device remains looked and in sleep mode.

What can I do in order to obtain an automatically wake up of my device when the alarm goes off?

EDIT:

I tried the following:

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();

KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); 
KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();

That seemed to work with the exception of this:

04-10 13:49:59.260: A/PowerManager(4292): WakeLock finalized while still held: TAG

Moreover, I have this warning on acquire method:

Found a wakelock acquire() but no release() calls anywhere

like image 480
GVillani82 Avatar asked Dec 27 '22 05:12

GVillani82


2 Answers

Try the below code in your Receiver Activity:

Window wind;
wind = this.getWindow();
wind.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
like image 106
dharam Avatar answered Jan 05 '23 08:01

dharam


Just to edit that after Android version 4.4, the setRepeating method has been deprecated and they have introduce setInexactRepeating().

Please go through this update blog for setting alarm: https://developer.android.com/training/scheduling/alarms.html

like image 42
markus Avatar answered Jan 05 '23 08:01

markus