Unlock the Screen Programmatically - android

I have a share button in the GCM notification. On click of the share button, I need to launch share intent. Everything works perfectly. Only problem that I'm facing is Lollipop lock screen feature. When I click share button from lock screen, my intent dialog appears below the lock screen and user has to unlock the screen to see the dialog. I want to unlock the screen programatically, when share button is clicked.
I tried with Power Manager, But all it's wakeClock flags are deprecated and WindowManager.LayoutParams.Flag_KEEP_SCREEN_ONis recommened to use. But I'm not using activity here. I'm using broadcastReciever context. and hence I cannot use getWindow()method.
I also tried with KeyguardManager. But even disableKeyguard() is deprectated.
I cannot use the Intent.ACTION_SCREEN_ON, as this should be used, if we want to perform any action after screen is unlocked.
i had used below intent to programmatically close the notification tray:
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mContext.sendBroadcast(it);
Is there a similar intent, that can be broadcasted to unlock the screen
Updated Code using DevicePolicyManager:
public static void handleShareBtnClick(Context context, String message) {
GcmHelper helper = new GcmHelper();
helper.shareMessage(context, message);
if(Utility.isLollypopAndAbove()){
helper.unlockLockScreen();
}
helper.launchShareforForAlert();
}
public void unlockLockScreen(){
DevicePolicyManager devicePolicyMngr= (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName compName=new ComponentName(mContext, DeviceAdminReceiver.class);
if(!devicePolicyMngr.isAdminActive(compName))
devicePolicyMngr.removeActiveAdmin(compName);
}
Even after using DevicePolicyManager, It's not unlocking my screen

Step 1: Add below code in your activity before setContentView(R.layout.example);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Step 2: Lock your mobile, then you will see activity in which you have added this code.
This will work even though your mobile is locked with pattern lock. This will work like a charm.

Related

Use BroadcastReceiver that asks a user if they wish to open app onReceive

I am trying to create an app that communicates with USB flash drive. I would like to have the user prompted and asked if they wish to open the app once the intent "android.hardware.usb.action.USB_DEVICE_ATTACHED" is received.
Currently, I have the onRecieve() set up so that it launches the main activity
#Override
public void onReceive(Context context, Intent intent) {
Intent startIntent = context
.getPackageManager()
.getLaunchIntentForPackage(context.getPackageName());
startIntent.setFlags(
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT |
Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
);
context.startActivity(startIntent);
}
I would like to have the user prompted for permission first, a good example of what I would like to achieve is ES File Explorer Like this
I would like to have the user prompted and asked if they wish to open the app once the intent "android.hardware.usb.action.USB_DEVICE_ATTACHED" is received.
The screen you want to show has to from your app so in fact is has already started, even if the user perceives it differently.
What you can do is start an activity when this broadcast is received (you need to be in activity context to be able to show stuff on the screen). Make the activity's background transparent and have it show a dialog on top that handles the user's input. From there you either start the next activity, or not.

Return back to App after clicking on Accessibility Service

Please Please read the question complete before marking it as duplicate or vague
On clicking of a button I want to redirect the user to accessibility settings of the android mobile. Where user can click on the accessibility settings of the application. Here is the code that I am using for the same:
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(dummyIntent, 1);
Problem: When user clicks on, I want that it should redirect back to my application and should not remain on the accessibility screen itself.
It's quite late but i had to make the same stuff. So my suggestion is to use onServiceConnected() overridden method in class that extends AccessibilityService . When user apply accessibility in the settings you can launch intent to desired activity inside onServiceConnected(). But you should keep in mind that after device reboot onServiceConnected() also called, so use some flag to make sure that is not user action.
try this :
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
ActivityContext.startActivityForResult(dummyIntent, 1);

how can i recall a hidden app from phone dialer?

My Final project requires that app must be hidden,and never opened again unless entering some code in phone dialer (ex: *123#) can you help me guys to do that task?
This is a bit tricky and it has its up and downs, but what you need to do basically is:
On app install, you need to programmatically disable the app Icon so you cannot open it manually.
Have a BroadcastReceiver registered with the PROCESS_OUTGOING_CALLS intent filter (don't forget to set the uses-permissions).
In the receiver, listen for every dialed number and when it matches yours you need to activate the App Icon again and then you start the activity with possibly extra data to handle it later.
After processing the data in your activity remember to deactivate the icon again.
To programmatically disable the icon use:
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(this, MainActivity.class);
packageManager.setComponentEnabledSetting(
componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP
);
To enable it:
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, MainActivity.class);
packageManager.setComponentEnabledSetting(
componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP
);
In your receiver to get the dialed number you need to use:
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// Validate and start your activity here
// To start an activity from a receiver you need to use the flag FLAG_ACTIVITY_NEW_TASK in your intent
}
Note: After hiding the icon programmatically you might want to finish() the activity so it closes automatically at first run.
P.S I have a working sample of this, so rest assured as I have tested it actually works, sadly I cannot spoon feed you in your final project. Don't hesitate to ask anything tho. Good luck
There is no such functionality in Android. You may be able to do it with a custom home screen, but there is no "hide this app" functionality in the default launcher.

handle device's default screen lock behaviour

I am using device default lock screen in my app for activating and deactivating screen lock. In my application I used a check box which shows that screen lock is activated or not.
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivity(intent);
I am receiving it callback in my DeviceAdminReceiver class.
Methods:onPasswordChanged onPasswordFailed onPasswordSucceeded
Now if user selects none or press back none of these methods get called. I am not able to identify is screen is locked or not ? I used OnActivityResult for handling callback in my Activity, it works fine for back pressed(resultcode 0) but gives same results for all other options.
I found this link which tells that it can't be handled externally.
Summary: I wants to handle screen lock options directly from my application.
if we set password quality on that case user can not able to select none password option
Might it will help you .
We can do this in this way
private ComponentName mDeviceAdmin;
private DevicePolicyManager mDPM;
mDPM.setPasswordQuality(mDeviceAdmin, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
mContext.startActivity(intent);

Using FLAG_SHOW_WHEN_LOCKED with disableKeyguard() in secured Android lock screen

The Context
Recently, I have been looking for reliable ways to control a secured Android Keyguard. Mainly to display a custom lock screen. I know that Google had stated custom lock screens are not officially supported by the platform and should expect things to break, however, with the existing APIs, I believe there must be ways to do this. I have done tons of research for about a week but still having problem here and there. What I have implemented, assuming a secured Keyguard is enabled, so far are,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a window) to be displayed on screen on, putting the Keyguard behind, and all unsafe actions are prevented. Notification panel is disabled, finishing the activity will bring up the Keyguard. I implemented as following in my lock screen activity.
#Override
public void onAttachedToWindow() {
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
KeyguardManager, KeyguardManager.KeyguardLock are deprecated classes, but they still work all the way to Jelly Bean. To do this, I have a Service that handles two things, holding a static KeyguardManager and the related objects, and have it hold a BroadcastReceiver to receive Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF. (all the objects are initialized properly)
For ScreenReceiver
public static synchronized void disableKeyguard() {
if ( isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.disableKeyguard();
isLocked = false;
}
}
public static synchronized void reenableKeyguard() {
if ( !isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.reenableKeyguard();
keyguardLock = null;
isLocked = true;
}
}
For BroadcastReceiver
#Override
public void onReceive( Context context, Intent intent ) {
if ( intent.getAction().equals(Intent.ACTION_SCREEN_ON) ) {
Intent start = new Intent(context, LockScreen.class);
start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(start);
} else if ( intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ) {
ScreenReceiverService.reenableKeyguard();
}
}
For LockScreenActivity, when the user had input the correct passcode,
window.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ScreenReceiverService.disableKeyguard();
finish();
The Problem
Things that works
ACTION_ON and ACTION_OFF are received reliably.
LockScreenActivity is shown before the Keyguard (without telephone state handling yet)
Notification cannot be pulled down, exiting the activity in any way would display the lockscreen.
Things that does not work
After I disable Keyguard and call finish(), my app exits and homescreen or the last activity before the screen went off is shown. However, whenever I press the Home Key, the Keyguard will flash into the screen, quickly dismissing itself immediately, and the normal Home Key function/event is not handled (will not return to homescreen after flashing). This is observed when I rapidly tapped the Home Key repeatedly.
I even looked into the Android source code to find out the Home Key handling, but it is never sent to third-party applications unless the window type is WindowManager.LayoutParams.TYPE_KEYGUARD or WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG, which will throw SecurityException on 4.0+ even it worked on earlier platforms. And for the Keyguard, I have declared DISABLE_KEYGUARD permission use this shouldn't be the problem. My guess is the flag FLAG_SHOW_WHEN_LOCKED will tell the system to handle to Keyguard in some ways that would conflict with other disable calls. Since this flag is mostly used for Alarm/SMS type application, which is to show limited information to the user, then dismiss themselves and bring up the Keyguard. But in my case, having the user unlock my lock screen then unlock the system lockscreen simply defeats the purpose of my app.
So the question is why would the Keyguard flashes whenever I press Home after I disabled it? Is there any workaround/solution for this issue?
P.S. Thank you for reading such a long question. This is my first time asking a question here, if there is anything that I did wrong, please tell me (i.e. format, grammar, code convention, tags, etc.). Also I had no experience with any programming knowledge, I started with Android before I know what Java is. So I have not taken any proper course/training yet, this community is awesome and often help people like I even if they are simple questions, and of course watching Google I/O videos, reading blogs, read others' code help me a lot. So please tolerate any dumb mistakes/obvious bugs/stupid questions. I am only 16. ^_^"
I have used this with some success in both Gingerbread and ICS to open my activity (via a background service which is starting it). In the activity being started:
#Override
public void onAttachedToWindow() {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I had the same problem for the click of HOME button while unlocking the device. This can be solved by reseting the password to blank (ie "") :
DevicePolicyManager devicePolicyManager;
ComponentName demoDeviceAdmin;
devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
devicePolicyManager.resetPassword("",DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
and then disabling the keygaurd :
this.keyGuardLock = ((KeyguardManager)getSystemService("keyguard")).newKeyguardLock("keyguard");
keyGuardLock.disableKeyguard();
Hope this solved your problem. \m/ keep coding!
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a
window) to be displayed on screen on, putting the Keyguard behind
I tried to get this but my activity always preceded by the system lock screen. isOrderdBroadcast() says that ACTION_SCREEN_NO is an ordered broadcast.
I added flag to the activity :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
in onAttachedView(). But still the system lock is getting the preference over my Custom screen lock activity.
How did you get your activity before the system lock screen?
EDIT
On a hindsight, I think my understanding of the lock screen concept was wrong. My broadcast receiver was getting the broadcast first. But what was showing before that was the system lock screen launched when SCREEN_OFF is received. Fixed that problem as of now.
But stumped by the ambiguity of home button behavior. This won't be a problem in post ICS devices as all hard buttons are discouraged.
In your LockScreenActivity, ending the validation code by finish(); kills the LockscreenActivity and thus the whole app. Instead of that, you could just launch back your main activity (or any other) like this :
startActivity(new Intent(LockScreenActivity.this, MainActivity.class));
If AOSP is in your control then you need to set the simple flag and keyguard() is gone for good.
Here is the details to do that, get into the file
"overlay/frameworks/base/packages/SystemUI/res/values/config.xml"
and search for "config_enableKeyguardService" then set the flag to false.
NO MORE keyGuard, pheww

Categories

Resources