启动/停止内置的Wi-Fi / USB绑定从code? [英] Start / stop built-in Wi-Fi / USB tethering from code?

查看:132
本文介绍了启动/停止内置的Wi-Fi / USB绑定从code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何启动或停止内置圈养在Android 2.2的从我的应用程序?

How can I start or stop the built-in tethering in Android 2.2 from my application?

推荐答案

有一个在 ConnectivityManager A非公开圈养的API。如上图所示,你可以使用反射来访问它。我想这对一些Android 2.2的手机,它适用于所有的人(我的HTC打开网络共享,但不显示此状态栏......,所以从另一端检查)。下面是一些粗糙的code发射调试的东西,打开圈养在USB0。

There is a non-public Tethering API in the ConnectivityManager. As shown above you can use reflection to access it. I tried this on a number of Android 2.2 phones, and it works on all of them (my HTC turns on tethering but does NOT show this in the status bar..., so check from the other end). Below is some rough code which emits debugging stuff and turns on tethering on usb0.

ConnectivityManager cman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

Method[] methods = cman.getClass().getDeclaredMethods();
for (Method method : methods) {
    if (method.getName().equals("getTetherableIfaces")) {
        try {
            String[] ifaces = (String[]) method.invoke(cman);
            for (String iface : ifaces) {
                Log.d("TETHER", "Tether available on " + iface);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (method.getName().equals("isTetheringSupported")) {
        try {
            boolean supported = (Boolean) method.invoke(cman);
            Log.d("TETHER", "Tether is supported: " + (supported ? "yes" : "no"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    if (method.getName().equals("tether")) {
        Log.d("TETHER", "Starting tether usb0");
        try {
            int result = (Integer) method.invoke(cman, "usb0");
            Log.d("TETHER", "Tether usb0 result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

请注意:此code要求下列权限才能正常工作:

Please note: this code requires the following permissions to work:

android.permission.ACCESS_NETWORK_STATE
android.permission.CHANGE_NETWORK_STATE

这篇关于启动/停止内置的Wi-Fi / USB绑定从code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆