如何在程序中打开和关闭Android设备屏幕?


此示例演示了如何在程序中打开和关闭Android设备屏幕。

步骤1 - 在Android Studio中创建一个新项目,转到文件⇒新建项目,并填写所有必需的详细信息以创建新项目。

步骤2 - 将以下代码添加到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>

步骤3 - 将以下代码添加到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>

步骤4 - 将以下代码添加到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() ;
   }
}

步骤5 - 将以下代码添加到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;
      }
   }
}

步骤6 - 将以下代码添加到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>

让我们尝试运行您的应用程序。我假设您已将您的实际Android移动设备连接到您的计算机。要从Android Studio运行应用程序,请打开项目中的一个活动文件,然后单击工具栏中的运行 图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 –


更新于:2019年7月30日

4K+ 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告