【问题标题】:How to notify android app when device is connected with USB port当设备与 USB 端口连接时如何通知 android 应用程序
【发布时间】:2020-11-10 17:41:57
【问题描述】:

我正在尝试计算移动设备与 USB 端口连接的次数。但我不想计算设备何时连接到 USB 端口仅用于为手机充电。在 Flutter 或 Java 中有什么方法可以做到这一点?

【问题讨论】:

标签: java android flutter dart


【解决方案1】:

有一个广播事件,如果您在application settings 中打开了Debug,那么当您插入 USB 时,您会在通知栏上看到一个错误

public void onReceive(Context context, Intent intent) {
    String str = intent.getAction();
    if (str.equals(UsbManager.ACTION_USB_STATE)) {
        Bundle extras = intent.getExtras();
        usbConnected = extras.getBoolean(UsbManager.USB_CONNECTED);
        ...
        ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    相关资源
    最近更新 更多