標籤:android blog http io ar os sp for java
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();}}}
很詳細了,源碼就不用上了吧。
android無線調試