在Android应用程序中设置以太网连接
在尝试为运行在Jellybean上的Android设备提供以太网连接时, 我使用ifconfig和route命令来设置以太网连接。 现在我试图从Android应用程序执行这些命令,但无法设置IP和网关地址。 还有其他执行这些命令的方法吗? 我使用了以下代码在Android应用程序中设置以太网连接,android,static,ip,ethernet,gateway,Android,Static,Ip,Ethernet,Gateway,在尝试为运行在Jellybean上的Android设备提供以太网连接时, 我使用ifconfig和route命令来设置以太网连接。 现在我试图从Android应用程序执行这些命令,但无法设置IP和网关地址。 还有其他执行这些命令的方法吗? 我使用了以下代码 public void root_command(String cmd) { try{ Process p=Runtime.getRuntime().exec("su"); DataOutputStream stre
public void root_command(String cmd)
{
try{
Process p=Runtime.getRuntime().exec("su");
DataOutputStream stream=new DataOutputStream(p.getOutputStream());
stream.writeBytes(cmd);
stream.writeBytes("exit \n");
p.waitFor();
}
catch(IOException e)
{
}
catch(InterruptedException e)
{
}
}
这些是命令
busybox ifconfig eth0 <ip_address> up
busybox route add default gw <gateway_address> eth0
busybox ifconfig eth0 up
busybox路由添加默认gw eth0
使用以下方法:
Read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] returncommand = new String[8];
returncommand=read_file(filename);
line1.setText(returncommand[0]);
line2.setText(returncommand[1]);
line3.setText(returncommand[2]);
String[] mycommand = {"ifconfig eth0" + " " + returncommand[0] + " " + "netmask" + " " + returncommand[1] + " ", "netcfg eth0 up",
"route add default gw" + " " + returncommand[2] + " " + "dev eth0"};
if (returncommand[0] != null & returncommand[1] != null & returncommand[2] != null)
RunasRoot(mycommand);
}
});
}
public void RunasRoot(String[] cmds) {
try {
java.lang.Process process = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
for (String tmpcmd : cmds) {
outputStream.writeBytes(tmpcmd + "\n");
}
outputStream.writeBytes("exit\n");
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
这是什么“以太网连接”?还有安卓设备我猜它不是手机?你有一个有以太网电缆输入的设备吗?你想把设备放在你的局域网里吗?是的,你说得对。我使用的是一款带有以太网端口的定制安卓平板电脑。我现在正在开发一个应用程序,可以执行上面的busybox命令,通过以太网连接到互联网。