![](/img/trans.png)
[英]How to enable USB tethering in android programmatically or guiding user to do that
[英]How I can enable USB Tethering programmatically on an Android 4.0 device?
我想在我的Android 4.0設備上從我的應用程序啟用USB網絡共享嗎? 以下代碼適用於Android 2.2,但不適用於4.0。 有人可以幫忙嗎?
int USBTethering(boolean b) {
try {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
Log.d(tag, "test enable usb tethering");
Method[] wmMethods = cm.getClass().getDeclaredMethods();
String str = "";
if (b)
str = "tether";
else
str = "untether";
for (Method method : wmMethods) {
Log.d("in usb tethering method",method.getName()+"<<nn>>");
if (method.getName().equals(str)) {
Log.d(tag, "gg==" + method.getName());
Log.d("in if", " case matches "+method.getName()+"and str is "+str);
try {
Integer code = (Integer) method.invoke(cm, "usb0");
// code = (Integer) method.invoke(cm, "setting TH");
Log.d(tag, "code===" + code);
return 1;
} catch (IllegalArgumentException e) {
Log.d(tag, "eroor== gg " + e.toString());
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.d(tag, "eroor== gg " + e.toString());
e.printStackTrace();
} catch (InvocationTargetException e) {
Log.d(tag, "eroor== gg " + e.toString());
e.printStackTrace();
}
}
}
return 0;
} catch (Exception e) {
Log.e(tag, "" + e);
return 0;
}
}
我有與此類似的代碼,它可以在Android 4.0(但僅在某些設備上)上工作。 不幸的是,對網絡共享的訪問是特定於供應商的。 我注意到的一件事是您沒有使嘗試調用的方法變得可訪問。 如果在您使用的設備上將該方法設為私有,則此方法無效。 嘗試添加:
method.setAccessible(true);
致電之前
Integer code = (Integer) method.invoke(cm, "usb0");
另一件事是接口名稱(在您的情況下為“ usb0”也是特定於供應商的。接口名稱在不同制造商的不同設備上是不同的。請確保已為要測試的設備獲得正確的接口名稱。 。
在我的根設備上,我可以使用它:(xamarin)
suProcess = Runtime.GetRuntime().Exec("su root service call connectivity 30 i32 1");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.