4

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

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • Possible duplicate of [can i wake up my android when it is not plugged in and sleeping?](https://stackoverflow.com/questions/5262641/can-i-wake-up-my-android-when-it-is-not-plugged-in-and-sleeping) – Tot Zam Nov 03 '17 at 14:40

2 Answers2

8

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);
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
dharmendra
  • 7,835
  • 5
  • 38
  • 71
2

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

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
markus
  • 31
  • 2