在Android中编程运行shell命令

在Android中编程运行shell命令,android,shell,android-studio,command,processbuilder,Android,Shell,Android Studio,Command,Processbuilder,我试图在应用程序中运行一个简单的shell命令,通过查找“tun0”接口来确定VPN是否正在运行 在Android Studio中,如果我adb shell进入手机并运行以下命令,我可以在连接到VPN时看到明显的界面 ifconfig tun0 sargo:/ $ ifconfig tun0 tun0 Link encap:UNSPEC inet addr:172.16.0.1 P-t-P:172.16.0.1 Mask:255.255.255.0

我试图在应用程序中运行一个简单的shell命令,通过查找“tun0”接口来确定VPN是否正在运行

在Android Studio中,如果我
adb shell
进入手机并运行以下命令,我可以在连接到VPN时看到明显的界面

ifconfig tun0

sargo:/ $ ifconfig tun0
tun0      Link encap:UNSPEC
          inet addr:172.16.0.1  P-t-P:172.16.0.1  Mask:255.255.255.0
          inet6 addr: fe80::e45e:1ae7:8ac9:449a/64 Scope: Link
          UP POINTOPOINT RUNNING  MTU:1380  Metric:1
          RX packets:58 errors:0 dropped:0 overruns:0 frame:0
          TX packets:72 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500
          RX bytes:14356 TX bytes:7662
因此,如果我在命令行上尝试这样的测试,我会得到期望的结果

sargo:/ $ if ifconfig tun0 2>&1 >/dev/null; then echo "1"; else echo "0"; fi
如何在我的应用程序中做到这一点。我应该提到我的设备没有根。我已经读了很多关于“Process Builder”的书,看起来很直截了当,但我真的无法让它工作

任何帮助都会受到欢迎

感谢您抽出时间阅读