how to show popup Activity or Dialog when phone is locked? - android

I am trying to show a activity or a dialog when the phone is locked.
I have tried using a WakeLock but it did not work and I can only see the activity once my phone is unlocked?
What is the proper way to do this?

To show activity without dismissing the keyguard try this:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView();

You should use the KeyGuardManager to unlock the device automatically and then acquire your Wake Lock.
KeyguardManager kgm = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardLock kgl = kgm.newKeyguardLock("Your Activity/Service name");
if(isKeyguardUp){
kgl.disableKeyguard();
isKeyguardUp = false;
}
wl.acquire(); //use your wake lock once keyguard is down.

To show a popup on top of a lock screen try this, from my other answer:
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
alertDialog.show();
To show activity on top of a lock screen, or basically remove the lock screen when activity is starts, try this:
public void onCreate(Bundle savedInstanceState){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
...
}
Both of those options require api 5+

Related

Activity does not start until the screen is turned on

My service starts an Activity like this:
Intent intent = new Intent(context, CallMonitor.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this happens in CallMonitor.onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
On most devices, the activity (called up from service) wakes up the device, turns on the screen and is displayed.
But - for example - on Galaxy Tab 4, the activity is only called if the screen is already switched on.
If the screen is switched off and the service calls up the activity, it is displayed with a delay - It will be displayed immediatly after turning the screen on.
There is also a voice output in the activity. When the Galaxy S4 is switched off, it will not be played back - but immediatly after turning the screen on again.
Any suggestions?
I don't want to use WakeLock!
It looks like that's a device limitation and I don't know how you could work around that without using a WakeLock. We have a really old piece of code which you could modify to your needs which pretty much always worked for us:
public static void bringToFront() {
try {
PowerManager pm = (PowerManager) MainActivity.getAppContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "ALARM");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) MainActivity.getAppContext().getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
if (MainActivity.getAppActivity() != null) {
MainActivity.getAppActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
} catch(Exception e){
e.printStackTrace();
Log.w(TAG, "bringToFront Err: "+e.toString());
}
}

Android app launches on opening lockscreen

I want my app or a part (any activity) of the app to be launched on opening the lockscreen, i.e, when we unlock the keypad of our phone, my activity should start running.. Thanks in advance. :D enter image description here
Try this one
A way to get unlock event in android?
It's using ACTION_USER_PRESENT receiver, but stil could help you.
To unlock the keyguard.
add this permission <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// createWakeLocks();
KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardLock kgl = kgm.newKeyguardLock("QuickPopup");
if (isKeyguardUp) {
kgl.disableKeyguard();
isKeyguardUp = false;
}
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// getWindow()
// .addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
//wakeDevice();
}

KeyguardManager fails to unlock kitkat screen

I've noticed very interesting behavior of Android that I cannot explain. I'm using the following code to wake up the phone and disable keyguard:
PowerManager.WakeLock mFullWakelock = mPowerManager.newWakeLock(
(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP),
LOCK_TAG
);
mFullWakelock.acquire();
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock(LOCK_TAG);
keyguardLock.disableKeyguard();
Imagine there is cycle of wake (programmatically)->disable keyguard (programmatically)->press power button (manually)->wake->disable keyguard. The cycle works great until I manually press Home button while the phone's keyguard is disabled. After that, phone does wake up but keyguard is no longer disabled programmatically. I'd appreciate any ideas!
The problem is that a keylock seems to expire whenever the user presses home button or opens a notification. So whenever this happens, you have to create a new keylock.
I used this solution and it worked great:
https://stackoverflow.com/a/14519861/4098821
I had the same problem and i solved it using reflection and making my application as system signed app.
Here's the code:
try{
Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor lockPatternUtilsConstructor =
lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
lockPatternUtilsConstructor.setAccessible(true);
Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(ChangeDeviceLockMode.this);
Method clearLockMethod = lockPatternUtils.getClass().getMethod("clearLock", boolean.class);
clearLockMethod.setAccessible(true);
Method setLockScreenDisabledMethod = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class);
setLockScreenDisabledMethod.setAccessible(true);
clearLockMethod.invoke(lockPatternUtils, false);
setLockScreenDisabledMethod.invoke(lockPatternUtils, true);
Toast.makeText(ChangeDeviceLockMode.this,"none", Toast.LENGTH_LONG).show();
}catch(Exception e){
System.err.println("An InvocationTargetException was caught!");
Throwable cause = e.getCause();
Toast.makeText(ChangeDeviceLockMode.this,"none--"+cause, Toast.LENGTH_LONG).show();
Toast.makeText(ChangeDeviceLockMode.this,"none--"+e, Toast.LENGTH_LONG).show();
}
You also need to add a permission in manifest
android:name="android.permission.ACCESS_KEYGUARD_SECURE_STORAGE" />
This permission requires app to be system signed.

Trouble Implementing custom lock screen (background activities issue)

Im building a custom home screen with a custom lockscreen.
When the screen turn off, I launch the lock screen (activity),
However, when the lock screen is killed (by "finish()"), it goes back to
the last activity in my homescreen apk, and not to the real activity (apk) that
was visible right before the screen went off.
For example, if i'm in Calculator application, or in Clock applicaiton, And the lock screen turns on, When the lock screen activity is finished, It doesn't return to Calculator/Clock
Here's where I register the lock screen (in the main launcher activity) for receiving screen on/off events:
private void doLockScreenOperations()
{
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
IntentFilter lockfiFilter = new IntentFilter();
lockfiFilter.addAction(Intent.ACTION_SCREEN_OFF);
lockfiFilter.addAction(Intent.ACTION_SCREEN_ON);
getApplicationContext().registerReceiver(new LockScreenReceiver(), lockfiFilter);
}
Here's the receiver itself, where I launch the lock screen's activity:
public class LockScreenReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(Intent.ACTION_SCREEN_OFF))
{
if (LockScreenActivity.isLockScreenAlive == false)
{
Intent lockIntent = new Intent(context, LockScreenActivity.class);
lockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(lockIntent);
}
}
else if (action.equals(Intent.ACTION_SCREEN_ON))
{
}
}
}
The LockScreenActivity is, at that moment, a simple activity with a button
that is called finish() when the button is clicked.
I have no idea how to fix this.
Thanks in advance!
I'm not entirely sure mate, but you have to consider this:
Intent.FLAG_ACTIVITY_NEW_TASK starts a fresh new group of views
so logically you don't have a previous activity to go back to when exiting your lock screen.
I'm building one myself, very similarly to how you do it actually.
but unfortunately having these problems:
-it loads to slow sometimes
-it loads whenever it "feels" like :/
nm, goodluck mate

How to lock a device in android

How to lock a android device or screen??. Here is my code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
Button b = new Button(context);
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("inside button");
PowerManager pManager = (PowerManager)
getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pManager.newWakeLock(PowerManager.
PARTIAL_WAKE_LOCK, "lock screen");
wl.acquire();
wl.release();
}
});
}
Here I am using a button. when user clicks on then the screen should be lock. But this code is not working. Is have to use BraodCastRecievr??. I included this permmission in manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
But also not working

			
				
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
For locking the screen use,
lock.reenableKeyguard();
and for disabling the lock use,
lock.disableKeyguard()
This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.
When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"
Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()
And of course we need to give permission android.permission.DISABLE_KEYGUARD
Try out the example shown here:
http://developer.android.com/reference/android/os/PowerManager.html
Lock the android device programmatically

Categories

Resources