android通过wifi进行程序的调试,程序需要root权限才可以。

1、首先加入权限

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

2、布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:id="@+id/btn"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/open_adb" />    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

3、JAVA文件代码

public class MainActivity extends Activity {private Button btn;private boolean isOpen;private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = (Button) findViewById(R.id.btn);tv = (TextView) findViewById(R.id.tv);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!isOpen) {tv.setText("打开命令行,cd命令进入安装SDK的目录中platform-tools文件夹,然后输入 adb connect"+ getLocalIpAddress() + ":5555");btn.setText("关闭");execShell("setprop service.adb.tcp.port 5555");execShell("start adbd");isOpen = true;} else {tv.setText("");btn.setText("打开");execShell("stop adbd");isOpen = false;}}});}private String getLocalIpAddress() {WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);WifiInfo wifiInfo = wifiManager.getConnectionInfo();// 获取32位整型IP地址int ipAddress = wifiInfo.getIpAddress();// 返回整型地址转换成“*.*.*.*”地址return String.format("%d.%d.%d.%d", (ipAddress & 0xff),(ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),(ipAddress >> 24 & 0xff));}public static String getIpAddress() {try {for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {NetworkInterface intf = en.nextElement();for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {InetAddress inetAddress = enumIpAddr.nextElement();if (!inetAddress.isLoopbackAddress()&& inetAddress instanceof Inet4Address) {// if (!inetAddress.isLoopbackAddress() && inetAddress// instanceof Inet6Address) {return inetAddress.getHostAddress().toString();}}}} catch (Exception e) {e.printStackTrace();}return null;}public void execShell(String str) {try {// 权限设置Process p = Runtime.getRuntime().exec("su");// 获取输出流OutputStream outputStream = p.getOutputStream();DataOutputStream dataOutputStream = new DataOutputStream(outputStream);// 将命令写入dataOutputStream.writeBytes(str);// 提交命令dataOutputStream.flush();// 关闭流操作dataOutputStream.close();outputStream.close();} catch (Throwable t) {t.printStackTrace();}}}

很详细了,源码就不用上了吧。

更多相关文章

  1. Android SDK/ADT 历史版本下载地址
  2. 【Android】Android 代码判断是否获取ROOT权限(一)
  3. Android SDK大连东软镜像地址及地址列表
  4. android sdk 离线下载地址及对应映射关系
  5. 【转】Android ADB命令集
  6. android环境变量设置以及adb的常用命令
  7. Android 使用Android Studio + Gradle 或 命令行 进行apk签名打
  8. android adb shell 命令大全
  9. android adb 命令

随机推荐

  1. Android网络编程-----从服务器端获取xml
  2. Android安全机制(2) Android Permission权
  3. 11、ffmpeg学习笔记—ffmpeg源码编译-And
  4. PullToRefreshListView addHeadView的正
  5. javaAndroid实现刚刚发表几天前的日期工
  6. Android SDK可以与JDK 1.7兼容吗?
  7. android开发中调用系统中分享功能的方法
  8. 我怎样才能获得移动方向
  9. Android核心分析 之十-------Android GWE
  10. 在string.xml中调用变量[重复]