SecurityException - "caller ... is not a delegate of scope delegation-block-uninstall" (Android 8 - Java)
SecurityException - "caller ... is not a delegate of scope delegation-block-uninstall" (Android 8 - Java)
我想编写一个具有安全功能的 Android 应用程序。最重要的功能是不允许用户卸载(或停止)该应用程序。
所以我找到了 DevicePolicyManager API,它提供了一个名为 setUninstallBlocked() 的方法。我尝试 运行 此功能,但应用程序崩溃并显示错误日志。您可以在这个附件中看到日志。
你知道哪里出了问题吗?
日志
2020-02-28 01:57:22.231 20440-20440/com.example.foralex E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.foralex, PID: 20440
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foralex/com.example.foralex.MainActivity}: java.lang.SecurityException: Caller with uid 10090 is not a delegate of scope delegation-block-uninstall.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.SecurityException: Caller with uid 10090 is not a delegate of scope delegation-block-uninstall.
at android.os.Parcel.readException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1888)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setUninstallBlocked(IDevicePolicyManager.java:6965)
at android.app.admin.DevicePolicyManager.setUninstallBlocked(DevicePolicyManager.java:6568)
at com.example.foralex.MainActivity.blockUninstallOfThisApp(MainActivity.java:206)
at com.example.foralex.MainActivity.onCreate(MainActivity.java:184)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
MainActivity.java
包 com.example.foralex;
public class MainActivity 扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: critical function!
blockUninstallOfThisApp("com.example.foralex");
}
/**
* Blocks the uninstall of the selected package
* ToDo: Need to work here!
*/
public void blockUninstallOfThisApp (String appStr){
DevicePolicyManager dpm = this.getSystemService(DevicePolicyManager.class);
// set the app to the state that it can not get uninstalled
dpm.setUninstallBlocked(null, appStr, true);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foralex">
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission
<uses-permission
android:name="Manifest.permission.BIND_DEVICE_ADMIN " />
<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="com.example.foralex.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</xml>
您应该将应用程序安装为 DeviceOwner 或 ProfileOwner 才能使用 DevicePolicyManager API。或者请 Do/Po 代表团也使用此 api。
请 google 为“Android 设备所有者/配置文件所有者”和“android 委派范围”,以便所有这些对您有意义。
祝你好运
我想编写一个具有安全功能的 Android 应用程序。最重要的功能是不允许用户卸载(或停止)该应用程序。
所以我找到了 DevicePolicyManager API,它提供了一个名为 setUninstallBlocked() 的方法。我尝试 运行 此功能,但应用程序崩溃并显示错误日志。您可以在这个附件中看到日志。
你知道哪里出了问题吗?
日志
2020-02-28 01:57:22.231 20440-20440/com.example.foralex E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.foralex, PID: 20440
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.foralex/com.example.foralex.MainActivity}: java.lang.SecurityException: Caller with uid 10090 is not a delegate of scope delegation-block-uninstall.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.SecurityException: Caller with uid 10090 is not a delegate of scope delegation-block-uninstall.
at android.os.Parcel.readException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1888)
at android.app.admin.IDevicePolicyManager$Stub$Proxy.setUninstallBlocked(IDevicePolicyManager.java:6965)
at android.app.admin.DevicePolicyManager.setUninstallBlocked(DevicePolicyManager.java:6568)
at com.example.foralex.MainActivity.blockUninstallOfThisApp(MainActivity.java:206)
at com.example.foralex.MainActivity.onCreate(MainActivity.java:184)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
MainActivity.java
包 com.example.foralex;
public class MainActivity 扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: critical function!
blockUninstallOfThisApp("com.example.foralex");
}
/**
* Blocks the uninstall of the selected package
* ToDo: Need to work here!
*/
public void blockUninstallOfThisApp (String appStr){
DevicePolicyManager dpm = this.getSystemService(DevicePolicyManager.class);
// set the app to the state that it can not get uninstalled
dpm.setUninstallBlocked(null, appStr, true);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.foralex">
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS" />
<uses-permission
<uses-permission
android:name="Manifest.permission.BIND_DEVICE_ADMIN " />
<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="com.example.foralex.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</xml>
您应该将应用程序安装为 DeviceOwner 或 ProfileOwner 才能使用 DevicePolicyManager API。或者请 Do/Po 代表团也使用此 api。
请 google 为“Android 设备所有者/配置文件所有者”和“android 委派范围”,以便所有这些对您有意义。
祝你好运