android 通过adb命令控制wifi开关
16lz
2021-01-25
android 通过adb命令控制wifi开关
2017年08月23日 17:47:46 刘国栋 阅读数:14598
版权声明:本文为博主原创文章,转载请声明文章出处。 https://blog.csdn.net/liu3364575/article/details/77509038
手机必须root
public static void setSettingsWifi() { ShellUtil.runRootCmd("svc wifi enable");}
package com.watch.in.uitlis.data;/** * Created by Administrator on 2017/6/21. */import android.util.Log;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class ShellUtil{ private static String TAG = ShellUtil.class.getName(); public static String runCommand(String command) { Process process = null; String result = "false"; try { process = Runtime.getRuntime().exec(command); process.waitFor(); result = inputStreamToString(process.getInputStream()); Log.e(TAG, result); } catch (Exception e) { return result; } finally { try { process.destroy(); } catch (Exception e) { } } return result; } //http://blog.csdn.net/alexander_xfl/article/details/9150971 //command can be some cmd, use ; to split public static String runRootCmd(String command) { return runRootCmd(command, ";"); } public static String runRootCmd(String command, String split) { Process process = null; DataOutputStream os = null; String result = "false"; try { process = Runtime.getRuntime().exec("su"); OutputStream outstream = process.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outstream); String temp = ""; String[] cmds = command.split(split); for(int i = 0; i < cmds.length; i++) temp += cmds[i] + "\n"; dataOutputStream.writeBytes(temp); dataOutputStream.flush(); dataOutputStream.writeBytes("exit\n"); dataOutputStream.flush(); process.waitFor(); result = inputStreamToString(process.getInputStream()); Log.i(TAG, temp); } catch (Exception e) { Log.e(TAG, e.getMessage()); return result; } finally { Log.i(TAG, result); try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { Log.e(TAG, e.getMessage()); } } return result; } private static String inputStreamToString(InputStream in) throws IOException { StringBuffer out = new StringBuffer(); byte[] b = new byte[1024]; for (int n; (n = in.read(b)) != -1;) { out.append(new String(b, 0, n)); } return out.toString(); }}
权限挑有用的就行
更多相关文章
- Gradle build 报错:Received status code 400 from server
- android系统属性读写操作SystemProperties
- android 根据apk文件获取包名等信息 已经判断 指定 这个apk 是否
- 初识Android(安卓)App Bundle
- android中电话拨号器,短信发送器
- 向sd写文件时权限
- Android锁屏状态下弹出activity,如新版qq的锁屏消息提示
- Android(安卓)Studio生成JavaDoc
- Security and Permissions 安全与权限 (一)