How to turn Android device screen on and off programmatically?

AndroidApps/ApplicationsMobile Development

This example demonstrate about How to turn Android device screen on and off programmatically.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
   xmlns: tools = "http://schemas.android.com/tools"
   android :layout_width= "match_parent"
   android :layout_height= "match_parent"
   android :layout_margin= "16dp"
   tools :context= ".MainActivity" >
   <LinearLayout
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :layout_centerInParent= "true"
      android :orientation= "horizontal" >
      <Button
         android :id= "@+id/btnEnable"
         android :layout_width= "0dp"
         android :layout_height= "wrap_content"
         android :layout_weight= "1"
         android :onClick= "enablePhone"
         android :text= "Enable" />
      <Button
         android :id= "@+id/btnLock"
         android :layout_width= "0dp"
         android :layout_height= "wrap_content"
         android :layout_weight= "1"
         android :onClick= "lockPhone"
         android :text= "Lock" />
   </LinearLayout>
</RelativeLayout>

Step 3 − Add the following code to res/xml/policies.xml

<? 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>

Step 4 − Add the following code to src/DeviceAdmin

package app.tutorialspoint.com.sample ;
import android.app.admin.DeviceAdminReceiver ;
import android.content.Context ;
import android.content.Intent ;
import android.widget.Toast ;
public class DeviceAdmin extends DeviceAdminReceiver {
   @Override
   public void onEnabled (Context context , Intent intent) {
      super .onEnabled(context , intent) ;
      Toast. makeText (context , "Enabled" , Toast. LENGTH_SHORT ).show() ;
   }
   @Override
   public void onDisabled (Context context , Intent intent) {
      super .onDisabled(context , intent) ;
      Toast. makeText (context , "Disabled" , Toast. LENGTH_SHORT ).show() ;
   }
}

Step 5 − Add the following code to src/MainActivity

package app.tutorialspoint.com.sample ;
import android.app.Activity ;
import android.app.admin.DevicePolicyManager ;
import android.content.ComponentName ;
import android.content.Context ;
import android.content.Intent ;
import android.support.annotation. Nullable ;
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
import android.view.View ;
import android.widget.Button ;
import android.widget.Toast ;
public class MainActivity extends AppCompatActivity {
   static final int RESULT_ENABLE = 1 ;
   DevicePolicyManager deviceManger ;
   ComponentName compName ;
   Button btnEnable , btnLock ;
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
      btnEnable = findViewById(R.id. btnEnable ) ;
      btnLock = findViewById(R.id. btnLock ) ;
      deviceManger = (DevicePolicyManager)
      getSystemService(Context. DEVICE_POLICY_SERVICE ) ;
      compName = new ComponentName( this, DeviceAdmin. class ) ;
      boolean active = deviceManger .isAdminActive( compName ) ;
      if (active) {
         btnEnable .setText( "Disable" ) ;
         btnLock .setVisibility(View. VISIBLE ) ;
      } else {
         btnEnable .setText( "Enable" ) ;
         btnLock .setVisibility(View. GONE ) ;
      }
   }
   public void enablePhone (View view) {
      boolean active = deviceManger .isAdminActive( compName ) ;
      if (active) {
         deviceManger .removeActiveAdmin( compName ) ;
         btnEnable .setText( "Enable" ) ;
         btnLock .setVisibility(View. GONE ) ;
      } else {
         Intent intent = new Intent(DevicePolicyManager. ACTION_ADD_DEVICE_ADMIN ) ;
         intent.putExtra(DevicePolicyManager. EXTRA_DEVICE_ADMIN , compName ) ;
         intent.putExtra(DevicePolicyManager. EXTRA_ADD_EXPLANATION , "You should enable the app!" ) ;
         startActivityForResult(intent , RESULT_ENABLE ) ;
      }
   }
   public void lockPhone (View view) {
      deviceManger .lockNow() ;
   }
   @Override
   protected void onActivityResult ( int requestCode , int resultCode , @Nullable Intent
   data) {
      super .onActivityResult(requestCode , resultCode , data) ;
      switch (requestCode) {
         case RESULT_ENABLE :
            if (resultCode == Activity. RESULT_OK ) {
               btnEnable .setText( "Disable" ) ;
               btnLock .setVisibility(View. VISIBLE ) ;
            } else {
               Toast. makeText (getApplicationContext() , "Failed!" ,
               Toast. LENGTH_SHORT ).show() ;
            }
         return;
      }
   }
}

Step 6 − Add the following code to androidManifest.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns: android = "http://schemas.android.com/apk/res/android"
   package= "app.tutorialspoint.com.sample" >
   <uses-permission android :name= "android.permission.CALL_PHONE" />
   <application
      android :allowBackup= "true"
      android :icon= "@mipmap/ic_launcher"
      android :label= "@string/app_name"
      android :roundIcon= "@mipmap/ic_launcher_round"
      android :supportsRtl= "true"
      android :theme= "@style/AppTheme" >
      <activity android :name= ".MainActivity" >
         <intent-filter>
            <action android :name= "android.intent.action.MAIN" />
            <category android :name= "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <receiver
         android :name= ".DeviceAdmin"
         android :description= "@string/app_description"
         android :label= "@string/app_name"
         android :permission= "android.permission.BIND_DEVICE_ADMIN" >
         <meta-data
            android :name= "android.app.device_admin"
            android :resource= "@xml/policies" />
         <intent-filter>
            <action android :name= "android.app.action.DEVICE_ADMIN_ENABLED" />
         </intent-filter>
      </receiver>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –


raja
Published on 07-Jun-2019 11:39:43
Advertisements