Android 5.1在APP內綁定使用的網路裝置
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
Log.d(TAG,"try to bind ethernet for ekioh");
Network[] allnet = cm.getAllNetworks();
if (allnet==null || allnet.length==0) {
Log.e(TAG,"Failed to getAllNetworks from ConnectivityManager");
} else {
boolean isBinded = false;
Log.d(TAG,"getNetworkInfo() : " + allnet.length);
for (Network net : allnet) {
NetworkInfo info = cm.getNetworkInfo(net);
LinkProperties lnk = cm.getLinkProperties(net);
if (lnk != null) {
Log.d(TAG, "Interface: " + lnk.getInterfaceName());
List
for (LinkAddress a : addr) {
Log.d(TAG, " addr: " + a.toString());
}
List
for (RouteInfo r : route) {
if (r.isDefaultRoute()) {
Log.d(TAG, " default route: content={" + r.toString() + "}");
} else {
Log.d(TAG, " route: content={" + r.toString() + "}");
}
}
} else {
Log.e(TAG,"LinkProperties is null");
}
if (info != null) {
Log.d(TAG, "Type: " + info.getTypeName());
Log.d(TAG, "State: " + info.getState());
Log.d(TAG, "isAvailable: " + info.isAvailable());
Log.d(TAG, "isConnected: " + info.isConnected());
if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
Log.d(TAG, "Found ethernet for binding");
//isBinded = cm.bindProcessToNetwork(net);
isBinded = ConnectivityManager.setProcessDefaultNetwork(net);
if (isBinded) {
Log.d(TAG,"Ethernet binded! bind network={" + net.toString() + "}");
break;
}
}
} else {
Log.e(TAG,"NetworkInfo is null");
}
}
if (!isBinded) {
Log.e(TAG,"Cannot bind eth0 for binding");
}
}
} else {
Log.e(TAG,"Failed to bind process to eth0");
}
留言