usb accessory, catch USB_ACCESSORY_ATTACHED by intent-filter of Service
android, usb
Solution
I am also exploring USB support on android for my project, and ended up browsing through Android source for this matter.
Yes USB_ACCESSORY_ATTACHED and USB_DEVICE_ATTACHED both intents can be caught only in activities but Not in Service. I could not find yet exactly why is it so. Like you, I believed it should be possible to directly notify a Service about USB state(device attached, accessory attached). But this is not the way it is implemented in Android. will post if I find something more on this.
Detach intents can be caught in BroadCastReceivers.
If you want to adventure yourself in Android source you can start from this directory - ICS_SOURCE/frameworks/base/services/java/com/android/server/usb and read from there.
Problem
In android usb accessory documentation, there is example of android manifest, where Activity catch USB_ACCESSORY_ATTACHED by intent-filter. Im asking myself, its possible to catch the same intent by intent-filter of Service/IntentService ? EDIT I tried this,but without success: ``` <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.entreprise.ws.main" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="com.entreprise.ws.main.WeatherStationClientActivity" android:exported="true" android:screenOrientation="portrait" > <!-- <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> --> </activity> <activity android:name=".EntryPointActivity" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.entreprise.ws.main.WiFiListActivity" android:label="@string/appname_wifilist" > </activity> <uses-library android:name="com.android.future.usb.accessory" /> <activity android:screenOrientation="portrait" android:name=".WSInstallatorActivity" android:exported="true" > </activity> <service class="services.MyService" android:name="services.MyService"> <intent-filter> <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /> <action android:name="android.hardware.usb.action.USB_ACCESSORY_DETACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" android:resource="@xml/accessory_filter" /> </service> </application> </manifest> ``` service: ``` package services; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { final static String TAG = "MyService"; @Override public void onCreate() { super.onCreate(); Log.i("TAG", "onCreate"); Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Log.i("TAG", "onDestroy"); Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent intent) { Log.i("TAG", "onBind"); // TODO Auto-generated method stub return null; } } ``` P.S for activity, this works fine EDIT2 I experimenting by moving intent-filter and meta-data to my broadcast receiver: I not receive no more attach event. What is funny, I continue receive detach event. Its looks like USB_ACCESSORY_ATTACHED works only with activity, despite "Broadcast Action" classification in documentation. EDIT3: Final conclusion Its looks like USB_ACCESSORY_ATTACHED can be caught only in activity (in reason of possible dialog with user ?! ). detach event can be catch in receivers.