我正在收听媒体安装和卸载事件,如下所示
<receiver
android:name=".activity.MediaManagementReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL_MOUNTED" />
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
</intent-filter>
</receiver>
当预定事件发生时,在onReceive
中,会发送LocalBroadcast
,应在MainActivity
@Override
public void onReceive(Context context, Intent intent) {
Intent localIntent;
Log.e(TAG+"-FIXME", "intent " + intent.getAction() + " ; data " + intent.getData().toString()+ " ; isSticky? "+isInitialStickyBroadcast() + ""+System.currentTimeMillis());
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT) |
intent.getAction().equalsIgnoreCase(Intent.ACTION_MEDIA_BAD_REMOVAL) |
intent.getAction().equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED)){
if(intent.getData().toString().contains("udisk"))
localIntent = new Intent(context.getPackageName() + ".USB.UNAVAILABLE");
else
localIntent = new Intent(context.getPackageName() + ".SD.UNAVAILABLE");
LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent);
}else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_MEDIA_MOUNTED)){
if(intent.getData().toString().contains("udisk"))
localIntent = new Intent(context.getPackageName() + ".USB.AVAILABLE");
else
localIntent = new Intent(context.getPackageName() + ".SD.AVAILABLE");
//
LocalBroadcastManager.getInstance(context).sendBroadcast(localIntent);
}
}
意图过滤器和LocalBroadcast注册完成如下
IntentFilter lbmIFilter = new IntentFilter();
lbmIFilter.addAction(getPackageName() + ".USB.AVAILABLE");
lbmIFilter.addAction(getPackageName() + ".USB.UNAVAILABLE");
lbmIFilter.addAction(getPackageName() + ".SD.AVAILABLE");
lbmIFilter.addAction(getPackageName() + ".SD.UNAVAILABLE");
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(getPackageName() + ".USB.AVAILABLE")) {
if (!isUSBAvailable()) {
Log.e(TAG, "FIXME calling onUSBPlugged "+System.currentTimeMillis());
usbAvailable = true;
onUSBPlugged();
}
} else if (intent.getAction().equalsIgnoreCase(getPackageName() + ".USB.UNAVAILABLE")) {
if (isUSBAvailable()) {
Log.e(TAG, "FIXME calling onUSBRemoved "+System.currentTimeMillis());
usbAvailable = false;
onUSBRemoved();
// cinemoMediaServer.StopUSBServer();
}
} else if (intent.getAction().equalsIgnoreCase(getPackageName() + ".SD.UNAVAILABLE")) {
Log.e(TAG, "FIXME calling onSDCARDRemoved "+System.currentTimeMillis());
onSDCARDRemoved();
} else if (intent.getAction().equalsIgnoreCase(getPackageName() + ".SD.AVAILABLE")) {
Log.e(TAG, "FIXME calling onSDCARDPlugged "+System.currentTimeMillis());
onSDCARDPlugged();
}
}
}, lbmIFilter);
问题是当usb快速插入和拔出时,会收到android.intent.action.MEDIA_EJECT
个事件并发送LocalBroadcast
。但LocalBroadcast
MainActivity