我怎样才能在Android 4.0设备上启用USB绑定编程? [英] How I can enable USB Tethering programmatically on an Android 4.0 device?

查看:131
本文介绍了我怎样才能在Android 4.0设备上启用USB绑定编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid 4.0设备支持USB数据连线,从我的应用程序?下面code工作为Android 2.2,但它并不适用于4.0工作。 任何人都可以帮忙吗?

  INT USBTethering(布尔B){
        尝试 {
            ConnectivityManager厘米=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            Log.d(标签,测试启动USB绑定);
            方法[] wmMethods = cm.getClass()getDeclaredMethods()。
            字符串str =;
            如果(B)的
                海峡=系带;
            其他
                海峡=untether;
            对于(方法方法:wmMethods){
                Log.d(在USB绑定方法,method.getName()+<< NN>>);
                如果(method.getName()。等于(STR)){
                    Log.d(标签,GG ==+ method.getName());
                    Log.d(中如果,大小写匹配+ method.getName()+和str是+ STR);
                    尝试 {
                        整数code =(整数)method.invoke(厘米,USB0);
                    // code =(整数)method.invoke(厘米,设置TH);
                        Log.d(标记code ===+ code);
                        返回1;
                    }赶上(抛出:IllegalArgumentException E){
                        Log.d(标签,eroor == GG+ e.toString());
                        e.printStackTrace();
                    }赶上(IllegalAccessException E){
                        Log.d(标签,eroor == GG+ e.toString());
                        e.printStackTrace();
                    }赶上(的InvocationTargetException E){
                        Log.d(标签,eroor == GG+ e.toString());
                        e.printStackTrace();
                    }
                }
            }
            返回0;

                       }赶上(例外五){
            Log.e(标记,+ E);
            返回0;
        }

    }
 

解决方案

我有code类似于此,它工作在Android 4.0(但仅限于某些设备)。不幸的是,获得束缚是非常特定于供应商。有一件事我注意到的是,你不要让你尝试调用访问的方法。如果您所使用的设备,该方法已经取得了私人,这是不行的。试着增加:

  method.setAccessible(真正的);
 

在打电话之前

 整数code =(整数)method.invoke(厘米,USB0);
 

另一件事是,接口名(你的情况USB0也是供应商特定的接口名称是来自不同厂商的不同设备不同。确保你有正确的接口名称设备您正在测试。

I want to enable USB Tethering from my app on my Android 4.0 Device? The following code works for Android 2.2 but it does not work for 4.0. Can anyone help?

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

    }

解决方案

I've got code that is similar to this and it works on Android 4.0 (but only on some devices). Unfortunately, access to tethering is very vendor-specific. The one thing I did notice is that you don't make the method you are trying to call accessible. If, on the devices you are using, the method has been made private, this won't work. Try adding:

method.setAccessible(true);

before you call

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

The other thing is that the interface name (in your case "usb0" is also vendor-specific. The interface name is different on different devices from different manufacturers. Make sure that you've got the correct interface name for the device you are testing.

这篇关于我怎样才能在Android 4.0设备上启用USB绑定编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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