Android Permission Denial: forceStopPackage()

风流意气都作罢 提交于 2019-12-01 11:40:17

问题


I am try to kill another application using forceStopPackage(). But my application shows runtime error.

Error :

java.lang.SecurityException: Permission Denial: forceStopPackage() from pid=10377, uid=10200 requires android.permission.FORCE_STOP_PACKAGES

In my manifest file i added the following permissions.

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

    <permission
        android:name="android.permission.FORCE_STOP_PACKAGES"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
        android:protectionLevel="signature"
        android:sharedUserId="android.uid.system" />

Code :

ActivityManager am = (ActivityManager) 
         context.getSystemService(Context.ACTIVITY_SERVICE);
Method forceStopPackage = am.getClass().
          getDeclaredMethod("forceStopPackage", String.class);  
    forceStopPackage.setAccessible(true);  
    forceStopPackage.invoke(am, packageName);

How to solve the problem.


回答1:


I know this is ab old thread but i wanted to make sure engineers visiting this thread has latest information.

android.permission.FORCE_STOP_PACKAGES is signature | priviliged permission hence it needs to go in to system partition under priv-app. So whoever wants to have this permission in their app have to work with respective OEM to pre-load them on to priv-app.

Starting from Android O: This permission along with App name needs to be declared in one of the permission xml

privapp-permissions-platform.xml – AOSP Apps privapp-permissions-google.xml – Google Apps privapp-permissions-.xml – OEM/ODM, OEM Associated and others

<privapp-permissions package="com.abc.android.xyz">
<permission name="android.permission.FORCE_STOP_PACKAGES"/>
</privapp-permissions> 



回答2:


You have to add the permission in the AndroidManifest.xml like below.

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

Make sure only if your app is signed with system key os will allow to stop other apps.



来源:https://stackoverflow.com/questions/20717708/android-permission-denial-forcestoppackage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!