Last active
March 20, 2016 16:24
-
-
Save marc0x71/260ac8a5f37d77e4a80d to your computer and use it in GitHub Desktop.
Lock and Unlock device during test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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