<receiver android:name="com.data_packet.RemovableDiskManagerReceiver"> <intent-filter> <action android:name="RemovableDiskManagerReceiver" /> <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <action android:name="android.intent.action.MEDIA_CHECKING" /> <action android:name="android.intent.action.MEDIA_EJECT" /> <action android:name="android.intent.action.MEDIA_REMOVED" /> <action android:name="android.intent.action.MEDIA_MOUNTED" /> <action android:name="android.intent.action.MEDIA_BAD_REMOVAL" /> <action android:name="android.intent.action.ACTION_MEDIA_SCANNER_STARTED" /> <action android:name="android.intent.action.ACTION_MEDIA_SCANNER_FINISHED" /> <data android:scheme="file" /> </intent-filter> </receiver>
若是是在註冊的靜態廣播通常必須含有以上的權限,這裏必須注意添加android
<data android:scheme="file" />ide
若是在代碼中註冊廣播則必須iFilter.addDataScheme("file")工具
這樣接受廣播判斷U盤get
public class RemovableDiskManagerReceiver extends BroadcastReceiver { public RemovableDiskManagerReceiver() { } @Override public void onReceive(Context context, Intent intent) { Log.e("U","接受到廣播"); // TODO Auto-generated method stub if( intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED )){ Intent serviceintent=new Intent(context,USBCopySeverice.class); serviceintent.setAction(TAG_SET.USB_INTENT.USB_ADD); context.startService(serviceintent); LogUtils.eLog("U盤工具","U盤插入!"); }else if(intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED) || intent.getAction().equals(Intent.ACTION_MEDIA_REMOVED)){ Intent serviceintent=new Intent(context,USBCopySeverice.class); serviceintent.setAction(TAG_SET.USB_INTENT.USB_OFFLINE); context.startService(serviceintent); LogUtils.eLog("U盤工具","U盤拔出!"); } }; }