【问题标题】:How to detected PC connection from an Android Application?如何从 Android 应用程序检测到 PC 连接?
【发布时间】:2017-03-14 17:01:04
【问题描述】:

当我检测到与 linux PC 的连接时,我正在尝试激活 USB 网络共享。 我已经找到了以下意图过滤器:ACTION_POWER_CONNECTED

是否有更具体的 Intent 过滤器来检测启用数据的连接?

如果不是,我如何检测数据连接?

ACTION_POWER_DISCONNECTED 将执行任务以停用网络共享。

谢谢

【问题讨论】:

    标签: android android-intent usb android-service android-usb


    【解决方案1】:

    我真的不明白为什么,但检查充电状态似乎可以完成这项工作。在下面的代码中,usbCharge 在简单充电器上为假,在电脑上为真。

    public void onReceive(final Context context, Intent intent) {
        IntentFilter extraFilterToGetBatteryInfo = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent extraIntentToGetBatteryInfo = context.getApplicationContext().registerReceiver(null, extraFilterToGetBatteryInfo);
    
        int chargePlug = extraIntentToGetBatteryInfo.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    
        if (usbCharge) {
            // Enable tethering
            Intent tetherSettings = new Intent();
            tetherSettings.setClassName("com.android.settings", "com.android.settings.TetherSettings");
            tetherSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(tetherSettings);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多