可能重复:
USB 连接通知
如果我想为所有 USB 电缆进行广播,在资源 XML 文件和清单中做什么。目前资源 XML 文件是这样的:
<resources>
<usb-accessory manufacturer="Acme, Inc" model="Whiz Banger" version="7.0" />
</resources>
将此添加到您的清单中
<receiver android:name=".YourReceiverClassName">
<intent-filter>
<action android:name="android.intent.action.UMS_CONNECTED" />
<action android:name="android.intent.action.UMS_DISCONNECTED" />
</intent-filter>
</receiver>
将您的接收器类名称中的 onReceive 修改为此
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
{
Log.d(TAG,"USB connected..");
}
if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
{
Log.d(TAG,"USB connected..");
}
}