[Solved] Show an activity or dialog over the lock screen

Put your problem here if it does not fit any of the other categories.

[Solved] Show an activity or dialog over the lock screen

Postby hal9000 » Mon Feb 22, 2010 9:44 pm

Hello all.

I'm new to this forum. I read it from some time ago, but until now I've found that the answer for my questions were already there.

Now I've been searching and I cannot find the answer, so finally I decided to register and ask the question by myself. What I wish to know is how can I show an activity or dialog over the lock screen?

I know it can be done, since Handcent SMS shows a dialog with the incoming SMS even if the device is locked.

Thanks.

EDIT: Well, I just received an SMS and Hancent doesn't display the dialog exactly over the lock screen, since what can be seen below the dialog is just the background used in home screen, but any way the dialog shows without the need to unlock the phone first, and that is what I want to achieve.
Last edited by hal9000 on Wed Feb 24, 2010 4:40 pm, edited 1 time in total.
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

Top

Postby hal9000 » Tue Feb 23, 2010 3:50 pm

Nobody knows? :(

I've been trying with different dialog types, but my dialog always shows "hidden" and I cannot see it until I unlock the phone.
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

Postby hal9000 » Wed Feb 24, 2010 4:36 pm

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. [;)]
Last edited by hal9000 on Tue Feb 08, 2011 2:42 pm, edited 1 time in total.
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

Postby hal9000 » Wed Feb 24, 2010 8:40 pm

Oops! I forgot to say that for this to work you need to add these permissions to manifest:

Code: Select all
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

lock screen in android

Postby adithi » Tue Dec 28, 2010 7:44 am

can anyone guide me? :(
i want to lock the android(froyo2.2) device programatically.i tried keyguard manager and device admin manager.but i cant find out the solution.
Thanks.
adithi
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 27, 2010 1:27 pm

Re:

Postby hal9000 » Mon Feb 07, 2011 1:58 pm

This is a PM I've received. I copy it here just in case it results useful for any body else:
varun640 wrote:Hi,

I have gone through your post (Show an activity or dialog over the lock screen) and it was very helpful. I have another problem and I think that you can be help me.
As soon as the user presses the Unlock button on his phone, I need to show him an activity/dialog. For Ex: winamp's lockscreen player to change to next/pre song which gets enabled as soon as user presses unlock button.

If you can help me please mail me your response to varun.aparoksha@gmail.com

Thanks,
varun


Ok, to do what you want you need to listen for ACTION_USER_PRESENT broadcast action, which is sent when the device detects the presence of an user after a wake up.
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

Top

Re: [Solved] Show an activity or dialog over the lock screen

Postby Chitra » Tue Feb 08, 2011 2:02 pm

Great Job!! :)
Thanks,
Chitra
User avatar
Chitra
Master Developer
Master Developer
 
Posts: 213
Joined: Mon Mar 01, 2010 7:59 am
Location: Bangalore

Re: [Solved] Show an activity or dialog over the lock screen

Postby ProfLayton » Wed Mar 07, 2012 5:31 am

I have this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
  2.                 final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
  3.                 kl.disableKeyguard();
  4.                
  5.                 PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
  6.                 PowerManager.WakeLock wl=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
  7.                 wl.acquire();
  8.                
  9.                 Builder adb = new AlertDialog.Builder(this);
  10.                 adb.setMessage("Testing");
  11.                 adb.setCancelable(false);
  12.                 adb.setNeutralButton("Close",new DialogInterface.OnClickListener(){
  13.                         @Override
  14.                         public void onClick(DialogInterface dialog, int which) {
  15.                                 kl.reenableKeyguard();
  16.                         }
  17.                 });
  18.                 AlertDialog ad = adb.create();
  19.                 ad.show();
  20.                
  21.                 wl.release();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


And nothing happens. Am i missing something?
ProfLayton
Freshman
Freshman
 
Posts: 8
Joined: Wed Dec 29, 2010 5:14 am

Re: [Solved] Show an activity or dialog over the lock screen

Postby hal9000 » Wed Mar 07, 2012 9:35 am

ProfLayton wrote:I have this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
  2.                 final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
  3.                 kl.disableKeyguard();
  4.                
  5.                 PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
  6.                 PowerManager.WakeLock wl=pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
  7.                 wl.acquire();
  8.                
  9.                 Builder adb = new AlertDialog.Builder(this);
  10.                 adb.setMessage("Testing");
  11.                 adb.setCancelable(false);
  12.                 adb.setNeutralButton("Close",new DialogInterface.OnClickListener(){
  13.                         @Override
  14.                         public void onClick(DialogInterface dialog, int which) {
  15.                                 kl.reenableKeyguard();
  16.                         }
  17.                 });
  18.                 AlertDialog ad = adb.create();
  19.                 ad.show();
  20.                
  21.                 wl.release();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


And nothing happens. Am i missing something?


First of all, are you sure you added the permissions?
Code: Select all
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />


And second thing, are you sure the code is executing? I mean, how are you launching the dialog when the screen is locked? Scheduled? Response to event?

EDIT: If your app doesn't need to run on API level 4 or below, then you can use FLAG_SHOW_WHEN_LOCKED and FLAG_KEEP_SCREEN_ON, although I'm not sure if the that second flag will turn on the screen when it's off or it just keep it on only if it was already on.
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

Re: [Solved] Show an activity or dialog over the lock screen

Postby ProfLayton » Thu Mar 08, 2012 1:09 am

I do have that.
I am trying to find out a way to execute it. Is there a certain broadcast that occurs that I can use a receiver to decipher?
ProfLayton
Freshman
Freshman
 
Posts: 8
Joined: Wed Dec 29, 2010 5:14 am

Re: [Solved] Show an activity or dialog over the lock screen

Postby hal9000 » Thu Mar 08, 2012 1:14 am

ProfLayton wrote:I am trying to find out a way to execute it. Is there a certain broadcast that occurs that I can use a receiver to decipher?

Well, I cannot tell that. It depends on what you're trying to do. But for testing purposes you could try to react to the plug/unplug of the power or something like that.
Announce: Invisible man looks for transparent woman to make never seen things.
User avatar
hal9000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Mon Feb 22, 2010 9:26 pm
Location: Spain

Re: [Solved] Show an activity or dialog over the lock screen

Postby ProfLayton » Thu Mar 08, 2012 1:39 am

Is there a way to tell when the unlock button is pressed?
ProfLayton
Freshman
Freshman
 
Posts: 8
Joined: Wed Dec 29, 2010 5:14 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests