在我的应用程序中,我需要检测空的otg插入。系统会检测到空的usb接口,并向其显示如下图所示的通知Notification Image
我需要领会它的意图。我尝试了下面的代码
<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>(此reciver位于活动内部)
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();
}
}connect或disconnected上没有烤面包。我还在我的活动应用程序清单中尝试了这些意图
<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>我怎么才能收到它呢?感谢就是进步
发布于 2016-09-15 21:56:20
在this question上,@chrisdowney发表了以下评论:
似乎已经停止在更高版本的安卓系统中发送
ACTION_UMS_CONNECTED(至少在ICS上是这样)。然而,从姜饼开始,你就可以收听"android.hardware.usb.action.USB_STATE"了,只要建立了USB连接,它就会广播,至少在我的实验中是这样。这里有一个额外的布尔值,"connected",来告诉你它是连接的还是断开的。因此,如果你同时监听它和ACTION_UMS_CONNECTED等,你的代码应该可以在所有版本上工作。- chrisdowney Jun 22 '12,21:46
看看这是否能解决你的问题。如果是,请支持他的评论;-)
https://stackoverflow.com/questions/39508094
复制相似问题