apk,静默安装,linux命令

import android.content.Context;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class LinuxCmdUtil {
	private static final String closeEth0="busybox ifconfig eth0 down";
	private static final String openEth0="busybox ifconfig eth0 up";

//	String val = execRootCmd("cat /sys/class/amhdmitx/amhdmitx0/cec_active");
	public  static void CloseEth0(){
		String val = execRootCmd(closeEth0);
	}
	public  static void openEth0(){
		String val = execRootCmd(openEth0);
	}
	public  static void startApp(){
		String startAppString="pm start -n [package+MainActivity]";
		String val = execRootCmd(startAppString);
	}
   	private static final String getCateWayIp=" cat /proc/net/arp";//获得网关Ip的Linux指令
   	public static String getCateWayIp(){
		String val = LinuxCmdUtil.execRootCmd(getCateWayIp);
		return val;
   	}
	/**
	 *重启
	 */
	public static void  restart(){
		LinuxCmdUtil.execRootCmd("reboot");

	}
	
	public  static void installApK(String installPath,Context mContext){
        // 核心是下面几句代码  
//        Intent intent = new Intent(Intent.ACTION_VIEW);  
//        intent.setDataAndType(Uri.fromFile(new File(installPath)),  
//                "application/vnd.android.package-archive");  
//        mainActivity.startActivity(intent);
		StringBuilder installCmdBuilder=new StringBuilder("pm install -r  ");
		installCmdBuilder.append(installPath);
		String val = execRootCmd(installCmdBuilder.toString());
	}
	
	public static String execRootCmd(String cmd) {

		String result = "";
		DataOutputStream dos = null;
		DataInputStream dis = null;
		try {
			Process p = Runtime.getRuntime().exec("su");
			dos = new DataOutputStream(p.getOutputStream());
			dis = new DataInputStream(p.getInputStream());


			dos.writeBytes(cmd + "\n");
			dos.flush();
			dos.writeBytes("exit\n");
			dos.flush();

			String line = null;
			while ((line = dis.readLine()) != null) {
				result += line;
			}
			p.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
}
linux命令执行可参考:http://www.jianshu.com/p/603f29a6a513
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值