Attempt to change component state security excepti

2020-02-14 05:38发布

问题:

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

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

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

回答1:

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.



回答2:

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



回答3:

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



回答4:

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.