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();    }}

 

 

 

 

权限挑有用的就行

更多相关文章

  1. Gradle build 报错:Received status code 400 from server
  2. android系统属性读写操作SystemProperties
  3. android 根据apk文件获取包名等信息 已经判断 指定 这个apk 是否
  4. 初识Android(安卓)App Bundle
  5. android中电话拨号器,短信发送器
  6. 向sd写文件时权限
  7. Android锁屏状态下弹出activity,如新版qq的锁屏消息提示
  8. Android(安卓)Studio生成JavaDoc
  9. Security and Permissions 安全与权限 (一)

随机推荐

  1. Android基础篇_Part1_Android基础知识
  2. Android(安卓)activity 转场动画Activity
  3. Android基础教程(二)之五大布局对象
  4. Android入门——Drawable与对应的资源xml
  5. android 浏览器插件开发 - 插件库
  6. 新浪OAuth同步方案(测试成功)
  7. 在AndroidStudio中使用GreenDAO
  8. android两屏幕互相滑动
  9. android之App widget实际应用Demo
  10. usetc oj --Android(安卓)key