如何使用 Kotlin 以编程方式打开和关闭 Android 设备屏幕?
此示例演示了如何使用 Kotlin 以编程方式打开和关闭 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" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="8dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="70dp" android:background="#008080" android:padding="5dp" android:text="TutorialsPoint" android:textColor="#fff" android:textSize="24sp" android:textStyle="bold" /> <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 - 将以下代码添加到 src/MainActivity.kt 中
import android.app.admin.DevicePolicyManager import android.content.ComponentName import android.content.Context import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Button import android.widget.Toast import androidx.annotation.Nullable import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { private val enableResult = 1 lateinit var deviceManger: DevicePolicyManager lateinit var compName: ComponentName lateinit var btnEnable: Button lateinit var btnLock: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" btnEnable = findViewById(R.id.btnEnable) btnLock = findViewById(R.id.btnLock) deviceManger = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager compName = ComponentName(this, DeviceAdmin::class.java) val active = deviceManger.isAdminActive(compName) if (active) { btnEnable.text = "Disable" btnLock.visibility = View.VISIBLE } else { btnEnable.text = "Enable" btnLock.visibility = View.GONE } } fun enablePhone(view: View) { val active = deviceManger.isAdminActive(compName) if (active) { deviceManger.removeActiveAdmin(compName) btnEnable.text = "Enable" btnLock.visibility = View.GONE } else { val intent = 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, enableResult) } } fun lockPhone(view: View) { deviceManger.lockNow() } override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { enableResult -> { if (resultCode == RESULT_OK) { btnEnable.text = "Disable" btnLock.visibility = View.VISIBLE } else { Toast.makeText( applicationContext, "Failed!", Toast.LENGTH_SHORT ).show() } return } } } }
步骤 4 - 创建一个新的 Kotlin 类 (DeviceAdmin.kt) 并添加以下代码 -
import android.app.admin.DeviceAdminReceiver import android.content.Context import android.content.Intent import android.widget.Toast class DeviceAdmin : DeviceAdminReceiver() { override fun onEnabled(context: Context, intent: Intent) { super.onEnabled(context, intent) Toast.makeText(context, "Enabled", Toast.LENGTH_SHORT).show() } override fun onDisabled(context: Context, intent: Intent) { super.onDisabled(context, intent) Toast.makeText(context, "Disabled", Toast.LENGTH_SHORT).show() } }
步骤 5 - 将以下代码添加到 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>
步骤 6 - 将以下代码添加到 androidManifest.xml 中
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q2"> <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: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 运行应用程序,请打开项目中的一个活动文件,然后点击工具栏中的运行图标 。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕。
广告