Ok. After more deeper researching I managed to do it, so I'll answer to myself and leave it here for anybody who needs it.
In order to show a dialog or activity even if the screen is locked you should use the class
KeyguardManager and the subclass
KeyguardLock. The
KeyguardLock has a method named
disableKeyguard which prevents the keyguard (name it "lock screen") from showing and even hide it if it was already showing. The complementary method
reenableKeyguard allows the screen to be locked again and in fact shows the lock screen if it was already showing when
disableKeyguard was called.
Here is a sample that shows a dialog after disabling the lock screen, and reenable it when the dialog is closed:
- Code: Select all
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl=km.newKeyguardLock("My_App");
kl.disableKeyguard();
ad_b=new AlertDialog.Builder(this);
ad_b.setMessage("Hi there!");
ad_b.setCancelable(false);
ad_b.setNeutralButton("Close", new DialogInterface.OnClickListener(){public void onClick(DialogInterface d, int i){kl.reenableKeyguard();}});
ad=ad_b.create();
ad.show();
In addition, if you want that your dialog or activity can be shown even if the screen is off, you can use
PowerManager and
WakeLock this way:
- Code: Select all
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl=km.newKeyguardLock("My_App");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
wl.acquire();
ad_b=new AlertDialog.Builder(this);
ad_b.setMessage("Hi there!");
ad_b.setCancelable(false);
ad_b.setNeutralButton("Close", new DialogInterface.OnClickListener(){public void onClick(DialogInterface d, int i){kl.reenableKeyguard();}});
ad=ad_b.create();
ad.show();
wl.release();
Hope it helps somebody. [;)]