以编程方式启用USB共享网络-有一个应用程序针对2.3

问题描述:

我在这里已经阅读了很多关于SO的问题,这些问题询问如何以编程方式启用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/apps/details?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?

使用以下代码,您可以启用USB绑定.我没有在4.0中进行测试.

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();
                        }
                    }
                }
        }