2012-11-12 147 views
0

我想在第三個應用中通過ActivityManager.RunningServiceInfo殺死另一個應用的服務器。 例如,我想要保存某些應用程序的服務,否則服務(不適合服務START_STICK)將被終止。 這裏是我的代碼:java.lang.SecurityException:不允許停止服務意圖?

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

然後總是提醒味精

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" 

如何解決? 謝謝

回答

2

您不能使用權限"android.permission.SHUTDOWN""android.permission.FORCE_STOP_PACKAGES",除非您的包使用系統證書籤名。有權限"android.permission.KILL_BACKGROUND_PROCESSES"將允許你只殺死你的進程。

+0

在您的應用程序的Android.mk文件中,您需要添加LOCAL_CERTIFICATE:= platform – Yury

+0

我真的忘記了這一點,非常感謝 –

相關問題