Java-Runtime

Runtime应用之:在安全环境中,在多任务操作系统中使用Java去执行其他程序(比如在Java中调用cmd shell去执行一些命令)

通过adb shell pm clear + 包名,可以清除apk包的缓存信息,但是需要通过Java程序去实现,具体实现类如下:

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 
 5 
 6 public class ExecCommand {
 7 
 8     /**
 9      * @ 调用cmd命令
10      */
11     private String command = null;
12     //构造函数
13     public ExecCommand(String command){
14         this.command = command;
15     }
16     
17     //调用cmd执行命令的函数
18     public void execCommand(String command) throws IOException{
19         Runtime runtime = Runtime.getRuntime();
20         Process p = runtime.exec(command);
21         StringBuffer stringBuffer = new StringBuffer();
22         try{
23             if(p.waitFor() != 0)
24             {
25                 System.err.println("exit value = " + p.exitValue());
26             }
27             BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
28             String line = null;
29             while((line = in.readLine()) != null){
30                 stringBuffer.append(line);
31             }
32             System.out.println(stringBuffer.toString());
33         }catch(Exception e){
34             System.err.println(e);
35         }finally{
36             try{
37                 p.destroy();
38             }catch(Exception e){    
39             }
40         }
41         
42     }
43     
44     /**
45      * @param args
46      * @throws IOException 
47      */
48     public static void main(String[] args) throws IOException {
49         // TODO Auto-generated method stub
50         String command = "adb shell pm clear xxx.xxx.xxx.xxx";
51         ExecCommand execCommand = new ExecCommand(command);
52         execCommand.execCommand(command);
53     }
54 
55 }

其中xxx.xxx.xxx.xxx代表具体的包名,这个程序如果执行成功的话,就会清除缓存,然后在eclipse的Console界面提示Success

原文地址:https://www.cnblogs.com/keke-xiaoxiami/p/4316401.html