How to Lock/Unlock Screen Programmatically

How to Lock/Unlock screen programmatically?

Edit:

As some folks needs help in Unlocking device after locking programmatically,
I came through post Android screen lock/ unlock programatically, please have look, may help you.

Original Answer was:

You need to get Admin permission and you can lock phone screen

please check below simple tutorial to achive this one

Lock Phone Screen Programmtically

also here is the code example..

LockScreenActivity.java

public class LockScreenActivity extends Activity implements OnClickListener {  
private Button lock;
private Button disable;
private Button enable;
static final int RESULT_ENABLE = 1;

DevicePolicyManager deviceManger;
ActivityManager activityManager;
ComponentName compName;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

deviceManger = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager)getSystemService(
Context.ACTIVITY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);

setContentView(R.layout.main);

lock =(Button)findViewById(R.id.lock);
lock.setOnClickListener(this);

disable = (Button)findViewById(R.id.btnDisable);
enable =(Button)findViewById(R.id.btnEnable);
disable.setOnClickListener(this);
enable.setOnClickListener(this);
}

@Override
public void onClick(View v) {

if(v == lock){
boolean active = deviceManger.isAdminActive(compName);
if (active) {
deviceManger.lockNow();
}
}

if(v == enable){
Intent intent = new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);
}

if(v == disable){
deviceManger.removeActiveAdmin(compName);
updateButtonStates();
}
}

private void updateButtonStates() {

boolean active = deviceManger.isAdminActive(compName);
if (active) {
enable.setEnabled(false);
disable.setEnabled(true);

} else {
enable.setEnabled(true);
disable.setEnabled(false);
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Admin enabled!");
} else {
Log.i("DeviceAdminSample", "Admin enable FAILED!");
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}

MyAdmin.java

public class MyAdmin extends DeviceAdminReceiver{  


static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(
DeviceAdminReceiver.class.getName(), 0);
}

static String PREF_PASSWORD_QUALITY = "password_quality";
static String PREF_PASSWORD_LENGTH = "password_length";
static String PREF_MAX_FAILED_PW = "max_failed_pw";

void showToast(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onEnabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: enabled");
}

@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "This is an optional message to warn the user about disabling.";
}

@Override
public void onDisabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: disabled");
}

@Override
public void onPasswordChanged(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw changed");
}

@Override
public void onPasswordFailed(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw failed");
}

@Override
public void onPasswordSucceeded(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw succeeded");
}

}

Unlock screen programmatically and show activity

Use KeyGuardLock from program

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguard = km.newKeyguardLock("MyApp");

to unlock screen

keyguard.disableKeyguard();

and dont forget to lock again after you have completed task

keyguard.reenableKeyguard();

use the following permission in the manifest

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" >
</uses-permission>

Lock or turn off the screen programmatically

Well something with high necessity can't finished with two lines of code, lock off screen required device admin. you may follow this :

private void lock() {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
if (pm.isScreenOn()) {
DevicePolicyManager policy = (DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE);
try {
policy.lockNow();
} catch (SecurityException ex) {
Toast.makeText(
this,
"must enable device administrator",
Toast.LENGTH_LONG).show();
ComponentName admin = new ComponentName(context, AdminReceiver.class);
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
context.startActivity(intent);
}
}
}

and AdminReceiverClass:

public class AdminReceiver extends DeviceAdminReceiver {
public static final String ACTION_DISABLED = "device_admin_action_disabled";
public static final String ACTION_ENABLED = "device_admin_action_enabled";

@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent(ACTION_DISABLED));
}
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
LocalBroadcastManager.getInstance(context).sendBroadcast(
new Intent(ACTION_ENABLED));
}
}

also we need declares the security policies used in metadata so for examples with Path android:resource="@xml/device_admin_sample" :

 <device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
<expire-password />
<encrypted-storage />
<disable-camera />
</uses-policies>
</device-admin>

in our case we just need :

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>`

Now declare it in our manifist.xml :

<receiver
android:name=".AdminReceiver"
android:label="@string/device_admin"
android:description="@string/device_admin_description"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>`

Hope it will Help you.

How to lock/unlock phone programmatically : Android

on BroadcastReceiver set up the wakelock and in the activity

Do This:

Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);

import following

import android.view.Window;
import android.view.WindowManager.LayoutParams;

How can I unlock screen programmatically when the notification action is clicked from lock screen in Android?

For enabling or disabling lock screen in Android, we need to get the instance of KeyguardManager

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);  
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);

and for disabling the lock use,

lock.disableKeyguard();

And of course we need the permission in the Manifest

android.permission.DISABLE_KEYGUARD

Programmatically lock and unlock iPhone (not jailbroken) screen?

We can't lock the screen programmatically without any private API but your requirement can achieve by sending keyboard events from paired bluetooth hardware devices.

Please check this link Force lock screen



Related Topics



Leave a reply



Submit