How to unlock Android 6.0 by code - android

I want to unlock my Android phone by programming. I used below code for Android 6.0 but it has some issue
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK
| PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Because the KeyguardLock is deprecated, thus I use below code
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
but it has an error
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6363)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:874)
at android.view.View.requestLayout(View.java:17483)
at android.view.View.setLayoutParams(View.java:11478)
at android.view.WindowManagerGlobal.updateViewLayout(WindowManagerGlobal.java:305)
at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:91)
at android.app.Activity.onWindowAttributesChanged(Activity.java:2596)
at android.support.v7.view.WindowCallbackWrapper.onWindowAttributesChanged(WindowCallbackWrapper.java:108)
at android.view.Window.dispatchWindowAttributesChanged(Window.java:852)
at com.android.internal.policy.impl.PhoneWindow.dispatchWindowAttributesChanged(PhoneWindow.java:4252)
at android.view.Window.setFlags(Window.java:825)
at android.view.Window.addFlags(Window.java:771)
How to fix it? Second, if my phone is locked by password, How can I unlock it? Thank you

Permission problem, you can add permission and grant this permission , because marshmallow need grant permission,your code is working before marshmallow perfect.

Related

How can I test system services?

I need a way to test system service in espresso.
Imaging to want test code below in espresso:
fingerprintManagerCompat = FingerprintManagerCompat.from(this);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (!fingerprintManagerCompat.isHardwareDetected()) {
showError("your device doesn't have finger print scanner");
}
So how I can test isHardwareDetected() method with false and true condition?
I don't have any idea what can I do in this situation, and how test this part of code.

switch off/on screen on PARTIAL_WAKE_LOCK

I'm using this code to enter in PARTIAL_WAKE_LOCK mode:
PowerManager pm = PowerManager.getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"screenWakeLock");
pm.acquire();
but I do not succeed to switch off the screen and switch on when I need it,I read tens of examples without succeed in it.
I can't use code that require the permission DEVICE_POWER like goToSleep() and wakeUp().
My goal is switch on the screen for 1 second and to switch off it for 10 seconds, and then start again.
Thanks all.
The use of PowerManager requires DEVICE_POWER permission that is only for applications that are signed by the same signature was used to sign the firmware. That is why you cannot use goToSleep() and wakeUp().
This code worked for me to turn on/off the screen:
//Turn off - brighness to 0;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);
To turn on just change the brighness to >0;

Android 4.3 : How can I check if user has lock enabled?

I want my app to behave differently (not store stuff) if the user does not have a lock screen or only swipe enabled.
The top answer here: check whether lock was enabled or not has been edited to say the code no longer works after upgrade to Android 4.3.
Is there anyway to detect this on Android 4.3?
Ok, so it seems it is possible with some reflection. Not the best solution, but at least it works on the devices we tried:
Class<?> clazz = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor<?> constructor = clazz.getConstructor(Context.class);
constructor.setAccessible(true);
Object utils = constructor.newInstance(context);
Method method = clazz.getMethod("isSecure");
return (Boolean)method.invoke(utils);
You can check it with KeyguardManager:
KeyguardManager keyguardMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if(keyguardMgr.isKeyguardSecure()){
// Secure keyguard, the keyguard requires a password to unlock
} else {
// Insecure keyguard, the keyguard doesn't require a password to unlock
}
You can check with KeyguardManager's method isKeyguardSecure() to make sure if lock or passcode is enabled or not.
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
Toast.makeText(this,"KeyGuardEnabled ? "+ keyguardManager.isKeyguardSecure(),Toast.LENGTH_LONG).show();

How to unlock phone using DevicePolicyManager

I know how to lock device using DevicePolicyManager..
boolean active = mDPM.isAdminActive(mDeviceAdminSample);
if (active) mDPM.lockNow();
but how to undo this(unlock a phone)?
Try this
KeyguardManager mKeyGuardManager = (KeyguardManager) DetSystemService(KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("MainActivity");
mLock.disableKeyguard();
http://developer.android.com/tools/testing/activity_testing.html#UnlockDevice
However, I am not satisfy myself with this solution as it uses deprecated methods... hope that someone knows more...

How to show Activity over lock screen in 4.2 jellybean while in sleep mode

i have implemented keyguard.wakelock to show activity on lock-screen(sleep mode) but
it show activity only in android version 2.2 and get crashed on above 2.3 version and some time on version 2.2 it show activity for few sec and then dis-appear over lock screen .so plz guide me to obtain this scenario working on every Android version .
here is the code i have used .
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
boolean iskeyguardopen =km.inKeyguardRestrictedInputMode();
//KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
Log.v("check key guard is enabled or not",""+km.inKeyguardRestrictedInputMode());
//setContentView(R.layout.customactivitypop);
count=1;
kl = km.newKeyguardLock("Taxi");
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();
wl.release();
Thank all.

Categories

Resources