首先获取ps指令的打印信息:

            
  1. private static String getPsPrint(IDevice device) { 
  2.         OutputStream os = new ByteArrayOutputStream(); 
  3.  
  4.         try { 
  5.             device.executeShellCommand("ps"
  6.                     new OutputStreamShellOutputReceiver(os)); 
  7.             os.flush(); 
  8.             return os.toString(); 
  9.         } catch (TimeoutException e) { 
  10.             e.printStackTrace(); 
  11.         } catch (AdbCommandRejectedException e) { 
  12.             e.printStackTrace(); 
  13.         } catch (IOException e) { 
  14.             e.printStackTrace(); 
  15.         } catch (ShellCommandUnresponsiveException e) { 
  16.             e.printStackTrace(); 
  17.         } 
  18.  
  19.         return ""
  20.     } 

其中OutputStreamShellOutputReceiver类:

            
  1. public class OutputStreamShellOutputReceiver implements IShellOutputReceiver { 
  2.  
  3.     OutputStream os; 
  4.      
  5.     public OutputStreamShellOutputReceiver(OutputStream os) { 
  6.         this.os = os; 
  7.     } 
  8.      
  9.     public boolean isCancelled() { 
  10.         return false
  11.     } 
  12.      
  13.     public void flush() { 
  14.     } 
  15.      
  16.     public void addOutput(byte[] buf, int off, int len) { 
  17.         try { 
  18.             os.write(buf,off,len); 
  19.         } catch(IOException ex) { 
  20.             throw new RuntimeException(ex); 
  21.         } 
  22.     } 
  23.  

获得ps的打印信息之后,需要根据进程名分析出pid:

            
  1. public static String parsePid(IDevice device, String exeName) { 
  2.  
  3.         String content = getPsPrint(device); 
  4.         String[] pidInfos = content.split("\n"); 
  5.         for (String info : pidInfos) { 
  6.             if (info != null) { 
  7.                 // shell 18867 18856 852 300 c022c0bc afe0cdec S ./gsnap 
  8.                 if (info.contains(exeName)) { 
  9.                     String[] fragments = info.split(" "); 
  10.                     int idx = 0
  11.                     for (String str : fragments) { 
  12.                         if ((str != null) && (!str.trim().equals(""))) { 
  13.                             idx++; 
  14.                         } 
  15.  
  16.                         if (idx == 2) { 
  17.                             return str; 
  18.                         } 
  19.                     } 
  20.                 } 
  21.             } 
  22.         } 
  23.  
  24.         return ""
  25.     } 

最后,根据得到的pid,杀掉此android进程:

            
  1. public static void killProcess(IDevice device, String pid) { 
  2.         // 旧的手机,缺乏grep和awk程序! 
  3.         //String killCmd = "kill -9 `ps | grep \"gsnap\" | grep -v \"grep\" | awk '{print $2}'`"; 
  4.         String killCmd = "kill -9 " + pid; 
  5.                  
  6.         try { 
  7.             device.executeShellCommand(killCmd, 
  8.                     new OutputStreamShellOutputReceiver(System.out)); 
  9.         } catch (TimeoutException e) { 
  10.             e.printStackTrace(); 
  11.         } catch (AdbCommandRejectedException e) { 
  12.             e.printStackTrace(); 
  13.         } catch (IOException e) { 
  14.             e.printStackTrace(); 
  15.         } catch (ShellCommandUnresponsiveException e) { 
  16.             e.printStackTrace(); 
  17.         } 
  18.     } 

之所以会这么复杂,是因为这个:

  1. // 旧的手机,缺乏grep和awk程序! 
  2.         //String killCmd = "kill -9 `ps | grep \"gsnap\" | grep -v \"grep\" | awk '{print $2}'`"; 

 

 

更多相关文章

  1. Android入门:HTML布局中Android程序与JAVASCRIPT的交互
  2. Android 获取手机网络状态
  3. Android检测手机是否插入/连接耳机
  4. android 启动应用程序
  5. 1.1使用内置的Camara应用程序捕捉图像
  6. Android通过包名打开手机应用商城寻找所指定App
  7. Android中调用locationManager.getProvider(LocationManager.GPS
  8. Android 获取imei号码,获取手机型号和系统版本号

随机推荐

  1. lua学习笔记 1 android 调用Lua, Lua脚本
  2. Android——PopupWindow
  3. Android(安卓)Gradle Plugin指南(三)——依
  4. AM335X Starter Kit Android(安卓)开发环
  5. android 中theme.xml与style.xml的区别
  6. 命令行装android
  7. Android控件属性大全
  8. 安卓相关学习
  9. Android下载文本文件和mp3文件
  10. VectorDrawable