Skip to content

Instantly share code, notes, and snippets.

@marc0x71
Last active March 20, 2016 16:24
Show Gist options
  • Save marc0x71/260ac8a5f37d77e4a80d to your computer and use it in GitHub Desktop.
Save marc0x71/260ac8a5f37d77e4a80d to your computer and use it in GitHub Desktop.
Lock and Unlock device during test
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
...
</manifest>
@BeforeClass
public static void setup() {
unlockDevice();
}
@AfterClass
public static void end() {
lockDevice();
}
private static void lockDevice() {
UiDevice uiDevice = UiDevice.getInstance(getInstrumentation());
try {
uiDevice.sleep();
} catch (RemoteException e) {
e.printStackTrace();
}
}
protected static void unlockDevice() {
UiDevice uiDevice = UiDevice.getInstance(getInstrumentation());
try {
uiDevice.wakeUp();
} catch (RemoteException e) {
e.printStackTrace();
}
Context app = getTargetContext().getApplicationContext();
KeyguardManager keyguard = (KeyguardManager) app.getSystemService(Context.KEYGUARD_SERVICE);
keyguard.newKeyguardLock("TAG").disableKeyguard();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment