Android的 - 启用USB网络编程 - 有一个应用程序,这样做是为了2.3这样做、应用程序、有一个、网络编程

2023-09-13 00:16:19 作者:旧街旧巷等新人

我读过这里这么多的问题是问如何以编程方式启用USB网络。

I've read many questions here on SO that ask how to enable USB tethering programmatically.

答案总是一样的,普通的应用程序无法做到这一点,只有系统的应用程序。

The answer is always the same, ordinary applications can't do it, only system apps.

然而,对于2.3,你可以下载在市场上的应用程序,会为你做它。

Yet for 2.3 you could download an app in the market that would do it for you.

https://play.google.com/store/应用程序/详细信息?ID = org.tdtran.autousbtethering

在ICS(Android的4.0.3),它不再有效。

On ICS (Android 4.0.3) it no longer works.

他们是怎么做到这一点的2.3?是否有可能也为4.0?

How did they do it for 2.3? Is it possible also for 4.0?

推荐答案

使用以下code可以启用USB绑定。我在4.0 didt测试。

using the following code you can enable USB tethering. i didt test in 4.0.

 public void switchOnTethering() {

                Object obj = getSystemService(Context.CONNECTIVITY_SERVICE);
                for (Method m : obj.getClass().getDeclaredMethods()) {

                    if (m.getName().equals("tether")) {
                        try {
                            m.invoke(obj, "usb0");
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
        }