首页 > 代码库 > android无线调试

android无线调试

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;	@Override	protected 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() {			@Override			public 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();		}	}}

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

 

android无线调试