Turn screen off programmatically
If your app is a device admin you can lock the screen with DevicePolicyManager.lockNow()
You can use this snippet (after setting the Device Admin part):
DevicePolicyManager manager = ((DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE));
manager.lockNow();
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.
Turn off the mobile screen in Android programming
You may find this useful https://developer.android.com/guide/topics/sensors/sensors_position.html
In the link the related program code you may want to look into is
@Override
protected void onResume() {
// Register a listener for the sensor.
super.onResume();
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
}
This might also be a repeated stackoverflow post to: Turn off screen programmatically when face is close the screen on Android
Hope this helps!
Related Topics
Android Studio - Textview Not Showing in Design View Layout
Android Textview Text Not Getting Wrapped
How to Clear Navigation Stack After Navigating to Another Fragment in Android
How to Restrict Entry of Numbers But Allow All the Special Characters and Alphabets in Edittext
Onclicklistener Does Not Work in Fragment
How to Create an Android View Pager With a Dots Indicator
How to Display a Loading Spinner While Getting Data from Firestore
How to Receive Fcm Push Notifications When App Is Killed
How to Remove Unnecessary Margins from Toolbar
How to Get the Current Time on a Button Click
How to Make Service That Is Unremovable Even If Killed by the System
How to Avoid Overlap View in Relative Layout in Android
How to Show Text in Lower Case in Android
Error: Open Failed: Enoent (No Such File or Directory)
Detect or Prevent If User Uses Fake Location
How to Get Minutes Difference Between Two Time in Android
Android Shared Preferences For Creating One Time Activity (Example)
How to Get the Value of a Listview Item Which Is Clicked in Android