1, Define a class derived from class BroadcastReceiver;
2, Register broadcast receiver;
MyBroadcastReceiver myReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
...
filter.addDataScheme("file"); //This is important. Otherwise, broadcast can'
t be received
registerReceiver(myReceiver, filter);
About other broadcast actions for media, and extra data bind to the intent, please refer to http://developer.android.com/reference/android/content/Intent.html (please search ACTION_MEDIA_ on this web page)
Notes: The path of mounted media is Intent.mData. Intent.mData is not available in SDK 1.0, but it be retrieved by calling Intent.getDataString();
阅读(1624) | 评论(0) | 转发(0) |