android 通过adb命令控制wifi开关

手机必须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();
    }
}




android:name="android.permission.CHANGE_NETWORK_STATE" />
android:name="android.permission.CHANGE_WIFI_STATE" />
android:name="android.permission.ACCESS_NETWORK_STATE" />
android:name="android.permission.ACCESS_WIFI_STATE" />
android:name="android.permission.WAKE_LOCK" />
android:name="android.permission.UPDATE_DEVICE_STATS" />
android:name="android.permission.INTERNET" />
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
android:name="android.permission.MODIFY_PHONE_STATE" />
android:name="android.permission.WRITE_SETTINGS" />
权限挑有用的就行


你可能感兴趣的:(android 通过adb命令控制wifi开关)