使用java反射时的java.lang.securityexception

时间:2013-01-24 11:45:18

标签: android reflection

我正在开发一个企业应用程序,我需要在代码中自动启用设备管理。

使用java反射,我尝试调用“setActiveAdmin”,这是“android.app.admin.DevicePolicyManager”中的隐藏函数。我有一个java.lang.securityexception。

Logcat:

01-24 17:07:58.390: W/System.err(1004): java.lang.SecurityException: Neither user 10080 nor current process has android.permission.BIND_DEVICE_ADMIN.
01-24 17:07:58.390: W/System.err(1004):     at android.os.Parcel.readException(Parcel.java:1321)
01-24 17:07:58.390: W/System.err(1004):     at android.os.Parcel.readException(Parcel.java:1275)
01-24 17:07:58.390: W/System.err(1004):     at android.app.admin.IDevicePolicyManager$Stub$Proxy.setActiveAdmin(IDevicePolicyManager.java:1507)
01-24 17:07:58.390: W/System.err(1004):     at com.marakana.android.devicepolicydemo.DemoDeviceAdminReceiver.onDisabled(DemoDeviceAdminReceiver.java:55)
01-24 17:07:58.390: W/System.err(1004):     at android.app.admin.DeviceAdminReceiver.onReceive(DeviceAdminReceiver.java:311)
01-24 17:07:58.390: W/System.err(1004):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1901)
01-24 17:07:58.390: W/System.err(1004):     at android.app.ActivityThread.access$2400(ActivityThread.java:122)
01-24 17:07:58.390: W/System.err(1004):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1052)
01-24 17:07:58.390: W/System.err(1004):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 17:07:58.390: W/System.err(1004):     at android.os.Looper.loop(Looper.java:132)
01-24 17:07:58.390: W/System.err(1004):     at android.app.ActivityThread.main(ActivityThread.java:4025)
01-24 17:07:58.390: W/System.err(1004):     at java.lang.reflect.Method.invokeNative(Native Method)
01-24 17:07:58.390: W/System.err(1004):     at java.lang.reflect.Method.invoke(Method.java:491)
01-24 17:07:58.390: W/System.err(1004):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-24 17:07:58.390: W/System.err(1004):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-24 17:07:58.390: W/System.err(1004):     at dalvik.system.NativeStart.main(Native Method)

在我的清单文件中,我给出了“android.permission.BIND_DEVICE_ADMIN”权限。 我仍然得到这个错误。如何解决这个问题?

已编辑:清单文件

<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".DevicePolicyDemoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Sample"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".DemoDeviceAdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <intent-filter>

            <!-- This action is required -->
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>


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

1 个答案:

答案 0 :(得分:0)

android.permission.BIND_DEVICE_ADMIN android:protectionLevel =“signature”,所以你的应用应该签署系统签名,否则你的应用无法获得android.permission.BIND_DEVICE_ADMIN