Detect USB tethering on android

后端 未结 5 458
Happy的楠姐
Happy的楠姐 2020-12-01 15:01

Is there any way to know (pro grammatically) in your Activity/Application that the user has enabled USB tethering on his phone?

5条回答
  •  无人及你
    2020-12-01 16:02

    Here is a solution to Listen for tethering state changes :

    First you need to be familiar with BroadcastReceiver. you can find a lot of tutorial (google : how to listen for connectivity changes ...)

    In order to get the Tethering state update, you need to use a hidden filter action of Android (see ConnectivityManager)

    and in your BroadcastReceiver class :

    IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");
    

    then register the filter to your BroadcastReceiver :

    myApplicationContext.registerReceiver(this, filter);
    

    on your onReceive(final Context context,final Intent intent) method, the Intent.extras information contains 3 arrays filled with the corresponding tethered network interface : erroredArray / availableArray / activeArray

    It's a little bit tricky but you can get the tethering status information.

    In addition, you can do some reflexion on hidden function of Android code :

    Search for getTetherableIfaces() in the Connectivity Manager.

    Here is a link : https://github.com/android/platform_frameworks_base/blob/master/core/java/android/net/ConnectivityManager.java#L1604

提交回复
热议问题