从本机打开设置DevicePolicyManager

时间:2018-10-13 18:13:21

标签: android react-native device-admin device-policy-manager react-native-native-module

我集成了DevicePolicyManager权限,以在用户从react native应用程序中单击按钮时打开设置。

问题, 当用户单击按钮我得到错误: “无法将ADD_DEVICE_ADMIN作为新任务启动”

代码:

RNMyFancyLibraryModule.java

 @ReactMethod
       public void lockAppEnable(){
            adminComponent = new ComponentName(mContext, SampleDeviceAdminReceiver.class);


                                       Intent activateDeviceAdmin = new Intent(
                                               DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

                                       activateDeviceAdmin.putExtra(
                                               DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                                               adminComponent);

                                       activateDeviceAdmin
                                               .putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                                                       "After activating admin, you will be able to block application uninstallation.");

                                        activateDeviceAdmin.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);


                                       mContext.startActivity(activateDeviceAdmin);

       }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.reactlibrary2">


    <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="26" />



    <application>



        <receiver
                android:name="com.reactlibrary2.SampleDeviceAdminReceiver"
                android:permission="android.permission.BIND_DEVICE_ADMIN" >

            <meta-data
                    android:name="android.app.device_admin"
                    android:resource="@xml/device_admin" />


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

    </application>

</manifest>

deviceAdmin.xml

<?xml version="1.0" encoding="utf-8"?>
<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>

如果我从外部运行本机代码(作为本机android应用程序),则该代码会很好用

0 个答案:

没有答案