以编程方式锁定或关闭屏幕



我想以编程方式关闭/锁定设备的屏幕

目前,当我尝试:

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

我有这个错误:

java.lang.SecurityException:uid 10176 拥有策略 #3 的活动管理员

这是我的安卓清单.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="opteamit.com.belami" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <application...

怎么了?

好吧,具有高度必要性的东西无法用两行代码完成,锁定屏幕所需的设备管理员。 您可以遵循以下内容:

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);
        }           
    }
}

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));
    }
}

我们还需要声明元数据中使用的安全策略,因此例如路径 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>

在我们的例子中,我们只需要:

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

现在在我们的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>`

希望它能帮助你。

请参阅下面的 user6490462 答案。

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);
       }           
   }
}

对于我们这些从谷歌登陆这里并且无法弹出管理员请求的人来说,我最终通过确保我从当前正在运行的活动启动活动来解决它(因此,调用不需要他们面前的context.。虽然有些人可能很幸运地从服务中获得了intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),但我不得不将呼叫移动到活动中。

ComponentName admin = new ComponentName(context, AdminReceiver.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
  .putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
startActivity(intent);

按照user6490462的说明进行操作后,这对我有用,但我不得不稍微调整意图

即使进行了这些更改,由于某种原因,我也无法弹出活动窗口以批准设备管理员权限。但是当我进入生物识别和安全 ->其他安全设置 ->设备管理应用程序时,我可以看到我的应用程序在那里等待其管理员权限的批准。

    ComponentName admin = new ComponentName(getApplicationContext(), AdminReceiver.class);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "Allow this app to turn screen off");
startActivity(intent);

相关内容

  • 没有找到相关文章

最新更新