如何在Android 4.0设备上以编程方式启用USB Tethering?

时间:2012-08-21 06:39:16

标签: android usb tethering

我想在我的Android 4.0设备上启用我的应用程序的USB Tethering?以下代码适用于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;
        }

    }

2 个答案:

答案 0 :(得分:1)

我有类似的代码,它可以在Android 4.0上运行(但仅适用于某些设备)。不幸的是,访问网络共享是特定于供应商的。我注意到的一件事是你没有使你试图调用的方法可访问。如果在您使用的设备上,该方法已设为私有,则无法使用。尝试添加:

method.setAccessible(true);

之前打电话

Integer code = (Integer) method.invoke(cm, "usb0");

另一个是接口名称(在您的情况下“usb0”也是特定于供应商的。接口名称在不同制造商的不同设备上是不同的。请确保您已获得设备的正确接口名称你正在测试。

答案 1 :(得分:0)

在我的root设备上,我得到了它:(xamarin)

var object = {
  id:id
  name:name
  age:age
};

https://android.stackexchange.com/questions/29954/can-i-change-some-android-settings-from-the-command-line

相关问题