
private class PingWifi implements Runnable { Process p = null; @OverrIDe public voID run() { // Todo auto-generated method stub /* * WifiInfo info=mWifiManager.getConnectionInfo(); int * ip=info.getIpAddress(); String ips=Formatter.formatIpAddress(ip); */ System.out.println("PingWifi runnable"); String[] shell = new String[] { "/system/bin/sh","-c"," " }; Runtime run = Runtime.getRuntime(); shell[2] = "Ping " + ipAddress; try { p = run.exec(shell); if (p.waitFor() != 0) { if (p.exitValue() == 1) { byte[] buff = new byte[1024]; inputStream is = p.getErrorStream(); int c; c = is.read(buff,1024); System.out.println("Ping error: " + new String(buff)); } } else { byte[] buff = new byte[1024]; inputStream is = p.getinputStream(); int c; c = is.read(buff,1024); System.out.println("Ping result: " + new String(buff)); // wait for harDWare } } catch (Exception e) { // Todo: handle exception e.printstacktrace(); } }voID stopProcess() { if (p != null) { p.destroy(); }else{ System.out.println("stopProcess: p==null"); } } }private PingWifi mPingWifi = new PingWifi(); Thread wifiThread = new Thread(mPingWifi); wifiThread.start(); 在onPause中我调用了mPingWifi.stopProcess(),结果是“stopProcess:p == null”.我仍然可以在“ps”列表中找到Ping进程.
我也想用“pkill Ping”命令.但是AndroID shell中不支持“pkill”命令.我的目标是Ping不是“停止”,直到“主页或后退”键事件发生.谢谢!
解决方法 尝试覆盖后退按钮并在其中调用Process.destroy().例:
public voID onBackpressed() {p.destroy();} 总结 以上是内存溢出为你收集整理的如何在android中停止run.exec()启动的进程全部内容,希望文章能够帮你解决如何在android中停止run.exec()启动的进程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)