2

I want to enable and disable Ethernet in android programatically. I have used following commands which are working on terminal but not on Java code.

ifconfig eth0 down
ifconfig eth0 up

and my code

public String executeCommand() {
    StringBuffer output = new StringBuffer();
    Process p;
    try {
        p = Runtime.getRuntime().exec("ifconfig eth0 down");
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line);
        }
    } catch (Exception e) {
        Log.d(TAG,"  exception "+e.toString());
        e.printStackTrace();
    }
    String response = output.toString();
    Log.d(TAG," response "+response);
    return response;
}  

After running this code, I have run cat /sys/class/net/eth0/operstate command in same code. It is working fine but Ethernet not disable.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
D.J
  • 1,439
  • 1
  • 12
  • 23

2 Answers2

0

I have not add Internet permission in manifest file.

D.J
  • 1,439
  • 1
  • 12
  • 23
0

Try something like this:

        String c = "ifconfig eth0 down\n";

        Process rproc = Runtime.getRuntime().exec("sh");
        DataOutputStream os = new DataOutputStream(rproc.getOutputStream());

        os.writeBytes(c);
        os.flush();

I'm looking to do something like this as well so if it works for you, I'll try it. ;) This command will likely require root access to work.

Dunfield
  • 785
  • 6
  • 11