Java 尝试在android中更改组件状态安全异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22277887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:41:09  来源:igfitidea点击:

Attempt to change component state security exception in android

javaandroidpermissions

提问by Aswan

hi i am working in framework side implementing one test app to disable another app but when i run my test app it is showing

嗨,我正在框架端工作,实现一个测试应用程序以禁用另一个应用程序,但是当我运行我的测试应用程序时,它显示

Java.lang.SecurityException: Permission Denial: attempt to change component state from pid=xxx,uid=xxxx,package uid=xxx
at xxxxxxxx

my java code

我的Java代码

PackageManager pm = getApplicationContext().getPackagemanager();
pm.setComponentEnabledSetting(new ComponentName("com.example","MainActivity"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);

AndroidManifest.xml

AndroidManifest.xml

<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE>

i have tried above permission with both (Permission is only granted to system apps) and also tried tag but no lucky

我已经尝试了以上权限(权限仅授予系统应用程序)并且还尝试了标记但没有幸运

how to use system apps granted permission,

如何使用授予权限的系统应用程序,

Could you please guys help me to resolve this issue

你能帮我解决这个问题吗

Thanks in advance

提前致谢

回答by JoxTraex

If you application is not a part of the system partition, then you will not be granted the permission. Get the app into the system partition and confirm that the PackageManager upon parsing your app during the dexopt grants you your desired permission.

如果您的应用程序不是系统分区的一部分,那么您将不会被授予权限。将应用程序放入系统分区,并确认 PackageManager 在 dexopt 期间解析您的应用程序时授予您所需的权限。

回答by Eduardo Jaramillo

My steps are:

我的步骤是:

  1. Disable package with command: pm disable "packageName"

  2. Enable packageName with command: pm enable packageName

  3. Launch principal activity if you know the name activity principal, Intent i = new Intent(Intent.ACTION_MAIN); i.setComponent(new ComponentName("namePackage", "namePackage.MainPrincipal")); i.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(i);

  1. 使用命令禁用包: pm disable "packageName"

  2. 使用命令启用 packageName: pm enable packageName

  3. 如果您知道名称活动主体,则启动主体活动, Intent i = new Intent(Intent.ACTION_MAIN); i.setComponent(new ComponentName("namePackage", "namePackage.MainPrincipal")); i.addCategory(Intent.CATEGORY_LAUNCHER); startActivity(i);

回答by Pampapathi

You can not Enable/disable app unless your app is system app

除非您的应用程序是系统应用程序,否则您无法启用/禁用应用程序

回答by Eric Cornelson

If your device is rooted, you can install your app as a system app by simply copying the apk into /system/app (/system/priv-app for 4.4+). This will allow you to use the "system only" permissions.

如果您的设备已 root,您只需将 apk 复制到 /system/app(4.4+ 为 /system/priv-app)即可将您的应用安装为系统应用。这将允许您使用“仅限系统”权限。