2012-11-30 52 views
0

可能重複:
USB Connection notification拿到轉播時,USB電纜連接

如果我想拿到轉播所有USB線纜什麼資源XML文件,並在明顯的做。 目前資源XML文件是這樣的:

<resources> 
<usb-accessory manufacturer="Acme, Inc" model="Whiz Banger" version="7.0" /> 
</resources> 
+0

檢查此http ://stackoverflow.com/a/5523092/603233 –

回答

2

添加到您的清單

<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> 

修改你的內心yourReceiverClassName的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.."); 
} 

}

+1

完美工作,但在ics或以上,我不能夠得到這個廣播,直到我打開我的應用程序。如果我不想在我的應用程序中放置任何活動,我想要做什麼。 – Abhishek

相關問題