Enabling USB tethering programmatically - there is an app that did it for 2.3

前端 未结 6 1822
野趣味
野趣味 2020-12-05 12:15

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 s

6条回答
  •  有刺的猬
    2020-12-05 13:10

    I know its an old thread but i hope this could help some other people in the future ,

    That code worked for me in Android 4.4(with root privilege)

    code: the trick is to use reflection , the method which changes the usb tethring is called "setUsbTethering"

    I wont write the entire class but here is what you need:

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Method usbTethering = connectivityManager.getClass().getMethod('setUsbTethering')
    int returnCode = (Integer)usbTethering.invoke(connectivityManager, true);
    
    0 = success
    

    you can print the entire class methods using the following code

    private static void printClassMethod(@NonNull Class aClazz) {
    Method[] wmMethods = aClazz.getDeclaredMethods();
      for (Method method : wmMethods) {
         Log.i('anytag', method.getName());
      }
    }
    

    then call printClassMethod(ConnectivityManager.class)

    More Over, you can go onto the class itself and check the methods and arguments needed.

    to get things work: you must set your app as System app,

    1. Declare in manifest : android:sharedUserId="android.uid.system"

    2. add writing secure permission

    3. Sign the apk using google certificate key(apk-with-system-privileges) and push it to /system/app

    Mind, that usbTethering is turned on while USB is attached, so a nicer solution will be to register to both USB_STATE and USB_ATTACH and enable/disable it correspondingly in onReceive Method. (USB tethering is turned off automatically when USB is de-attached)

提交回复
热议问题