如何在没有 root 的情况下禁用 Android 中的屏幕?

How disable screen in Android without root?

我想在按下按钮时禁用屏幕 (Android 4.4.2)。如果不对设备进行生根,这可能吗?我试过下面的代码,但它不起作用(抛出异常)。

private DevicePolicyManager mDPM;

创建时:

mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

按钮侦听器:

mDPM.lockNow();

AndroidManifest.xml:

<intent-filter> <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" /> </intent-filter>

堆栈跟踪:

10-03 20:32:50.187 4172-4172/com.microchip.android.BasicAccessoryDemo_API12 E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.microchip.android.BasicAccessoryDemo_API12, PID: 4172 java.lang.SecurityException: No active admin owned by uid 10175 for policy #3 at android.os.Parcel.readException(Parcel.java:1465) at android.os.Parcel.readException(Parcel.java:1419) at android.app.admin.IDevicePolicyManager$Stub$Proxy.lockNow(IDevicePolicyManager.java:1594) at android.app.admin.DevicePolicyManager.lockNow(DevicePolicyManager.java:1076) at com.microchip.android.BasicAccessoryDemo_API12.PasswordActivity.onClick(PasswordActivity.java:209) at android.view.View.performClick(View.java:4442) at android.view.View$PerformClick.run(View.java:18471) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) at dalvik.system.NativeStart.main(Native Method)

我找到了一个可以锁定设备(如果它有代码保护)并因此禁用屏幕的代码。下面是代码。

在清单上放置以下代码:

   <receiver
        android:name=".MyDeviceAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/my_admin" />

        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
    </receiver>

将此声明放在 onCreate 的 Activity:

static final int RESULT_ENABLE = 1;

将以下代码放入按钮侦听器中:

// Enable the Administrator mode.
DevicePolicyManager deviceManger = (DevicePolicyManager) getSystemService(
        Context.DEVICE_POLICY_SERVICE );
ActivityManager activityManager = (ActivityManager) getSystemService(
        Context.ACTIVITY_SERVICE );
ComponentName compName = new ComponentName( getApplicationContext( ),
        MyDeviceAdminReceiver.class);
Intent device_policy_manager_Int = new Intent( DevicePolicyManager
        .ACTION_ADD_DEVICE_ADMIN );
device_policy_manager_Int.putExtra( DevicePolicyManager.EXTRA_DEVICE_ADMIN,
        compName );
device_policy_manager_Int.putExtra( DevicePolicyManager.EXTRA_ADD_EXPLANATION,
        "Additional text explaining why this needs to be added." );
startActivityForResult( device_policy_manager_Int, RESULT_ENABLE );

// Check if the Administrator is enabled.
boolean active = deviceManger.isAdminActive(compName);

if ( active ) {
    Log.i("isAdminActive", "Admin enabled!");

    // If admin is enable - Lock device.
    deviceManger.lockNow( );
}