|
|
@@ -2,9 +2,14 @@ package com.bonait.bnframework.common.utils; |
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
import android.net.ConnectivityManager; |
|
|
|
import android.net.Network; |
|
|
|
import android.net.NetworkCapabilities; |
|
|
|
import android.net.NetworkInfo; |
|
|
|
import android.net.NetworkRequest; |
|
|
|
import android.os.Build; |
|
|
|
import android.util.Log; |
|
|
|
|
|
|
|
import java.io.DataOutputStream; |
|
|
|
|
|
|
|
/** |
|
|
|
* Created by LY on 2019/1/4. |
|
|
@@ -127,4 +132,81 @@ public class NetworkUtils { |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 绑定有线网络 |
|
|
|
* @param context |
|
|
|
*/ |
|
|
|
public static void connetEnternet(Context context) |
|
|
|
{ |
|
|
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
|
|
|
NetworkRequest.Builder builder = new NetworkRequest.Builder(); |
|
|
|
builder.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET); |
|
|
|
//builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
|
|
|
NetworkRequest networkRequest = builder.build(); |
|
|
|
connManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() { |
|
|
|
@Override |
|
|
|
public void onAvailable(Network network) { |
|
|
|
super.onAvailable(network); |
|
|
|
connManager.bindProcessToNetwork(network); // 绑定应用程序到有线网络 |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 绑定有线网络 |
|
|
|
* @param context |
|
|
|
*/ |
|
|
|
public static void connetWifi(Context context) |
|
|
|
{ |
|
|
|
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
|
|
|
NetworkRequest.Builder builder = new NetworkRequest.Builder(); |
|
|
|
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
|
|
|
NetworkRequest networkRequest = builder.build(); |
|
|
|
connManager.requestNetwork(networkRequest, new ConnectivityManager.NetworkCallback() { |
|
|
|
@Override |
|
|
|
public void onAvailable(Network network) { |
|
|
|
super.onAvailable(network); |
|
|
|
connManager.bindProcessToNetwork(network); // 绑定应用程序到有线网络 |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 发送 |
|
|
|
* @param cmds |
|
|
|
*/ |
|
|
|
public static void exec3(String... cmds) { |
|
|
|
StringBuffer command = new StringBuffer(); |
|
|
|
if (cmds.length <= 0) { |
|
|
|
return; |
|
|
|
} |
|
|
|
for (String cmd : cmds) { |
|
|
|
command.append(cmd).append("\n"); |
|
|
|
} |
|
|
|
Process process = null; |
|
|
|
DataOutputStream os = null; |
|
|
|
try { |
|
|
|
process = Runtime.getRuntime().exec("su"); |
|
|
|
// process = Runtime.getRuntime().exec("system/bin/su"); |
|
|
|
os = new DataOutputStream(process.getOutputStream()); |
|
|
|
os.write(command.toString().getBytes()); |
|
|
|
os.writeBytes("exit\n"); |
|
|
|
os.flush(); |
|
|
|
process.waitFor(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
Log.e("eeee",""+e.toString()); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
os.close(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
if(process!=null){ |
|
|
|
process.destroy(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |