gpt4 book ai didi

android - OTG USB 连接器 Intent

转载 作者:行者123 更新时间:2023-11-29 19:33:48 25 4
gpt4 key购买 nike

在我的应用程序中,我需要检测空的 otg 插入。系统通过此检测到空的 USB 连接器并显示如下图所示的通知 Notification Image

我需要接收它的 Intent 。我尝试了以下代码

<receiver android:name=".Otg">
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
<action android:name="android.intent.action.UMS_DISCONNECTED" />
</intent-filter>
</receiver>

(这个接收者在一个 Activity 中)

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
{
Toast.makeText(this, "connected" , Toast.LENGTH_LONG).show();
}

if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
{
Toast.makeText(this, "disconnected" , Toast.LENGTH_LONG).show();
}

}

连接或断开连接时没有任何内容。我也在我的 Activity 应用程序 list 中尝试了这些 Intent

<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.UMS_CONNECTED" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.UMS_DISCONNECTED" />
</intent-filter>

我怎样才能收到它?谢谢提前

最佳答案

this question ,@chrisdowney 发表了以下评论:

Seems like ACTION_UMS_CONNECTED has stopped being sent in later versions of Android (at least from ICS). However, from Gingerbread on you can listen for "android.hardware.usb.action.USB_STATE", this is broadcast whenever the USB connection is made, at least as far as my experimentation goes. There's a boolean extra, "connected", to tell you whether it's connected or disconnected. So if you listen for both that and ACTION_UMS_CONNECTED etc, your code should work on all versions. – chrisdowney Jun 22 '12 at 21:46

看看这是否能解决您的问题。如果是这样,请为他的评论点赞 ;-)

关于android - OTG USB 连接器 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39508094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com