我希望在第三个应用程序中通过ActivityManager.RunningServiceInfo杀死另一个应用程序的服务器。 例如,我想保留一些应用程序的服务,否则服务(不适合服务START_STICK)将被杀死。 这是我的代码:
ActivityManager actManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List killapplist = actManager.getRunningAppProcesses();
List killservicelist = actManager.getRunningServices(100);
for (Object aKillapplist : killapplist) {
ActivityManager.RunningAppProcessInfo localRunningAppProcessInfo = (ActivityManager.RunningAppProcessInfo)
aKillapplist;
String appname = localRunningAppProcessInfo.processName;
if (localRunningAppProcessInfo.pkgList != null) {
for (Object aKillservicelist : killservicelist) {
ActivityManager.RunningServiceInfo localRunningServiceInfo = (ActivityManager.RunningServiceInfo) aKillservicelist;
//some condition start
try {
Intent intentstop = new Intent();
intentstop.setComponent(localRunningServiceInfo.service);
mContext.stopService(intentstop);
} catch (SecurityException e) {
e.printStackTrace();
}
//some condition end
}
}
}
} else {
throw new MyException("kill list is empty");
}
然后总是得到警告msg
WARN/ActivityManager(363): Permission Denial: Accessing service ComponentInfo
WARN/System.err(5992): java.lang.SecurityException: Not allowed to stop service Intent
是sys签名的问题? 我使用了一些许可
uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"
uses-permission android:name="android.permission.SHUTDOWN"
uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"
如何解决? 感谢
答案 0 :(得分:2)
除非您的软件包已使用系统证书签名,否则您无法使用权限"android.permission.SHUTDOWN"
和"android.permission.FORCE_STOP_PACKAGES"
。拥有权限"android.permission.KILL_BACKGROUND_PROCESSES"
将允许您仅杀死您的进程。