Android设备6.0获取u盘挂载广播
2018-05-12 本文已影响0人
隔壁家de老王
一,清单文件静态注册广播
<receiver android:name=".receiver.UsbReceiver">
<intent-filter >
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<action android:name="android.os.storage.action.VOLUME_STATE_CHANGED" />
</intent-filter>
</receiver>
二,在广播中通过VolumeInfo判断挂载过程和卸载过程
public class UsbReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(VolumeInfo.ACTION_USB_DEVICE_ATTACHED)) {
Log.d("action===", "装载");
} else if (action.equals(VolumeInfo.ACTION_USB_DEVICE_DETACHED)) {
Log.d("action===", "卸载USB");
Toast.makeText(context, "USB设备移除", Toast.LENGTH_SHORT).show();
System.exit(0);
} else if (action.equals(VolumeInfo.ACTION_VOLUME_STATE_CHANGED)) {
int intExtra = intent.getIntExtra(VolumeInfo.EXTRA_VOLUME_STATE, VolumeInfo.STATE_UNMOUNTED);
if (intExtra == VolumeInfo.STATE_MOUNTED) {
Toast.makeText(UiUtils.getContext(), "发现USB设备", Toast.LENGTH_LONG).show()
}
}
}
}
public class VolumeInfo {
/**
* U盘挂载类型 插入U盘 STATE_UNMOUNTED->STATE_MOUNTED;拔出U盘 STATE_EJECTING->STATE_UNMOUNTED->STATE_BAD_REMOVAL
*/
//卸载
public static final int STATE_UNMOUNTED = 0;
//状态监测
public static final int STATE_CHECKING = 1;
//挂载完成
public static final int STATE_MOUNTED = 2;
//只读
public static final int STATE_MOUNTED_READ_ONLY = 3;
//格式化
public static final int STATE_FORMATTING = 4;
//开始移除
public static final int STATE_EJECTING = 5;
//无法挂载
public static final int STATE_UNMOUNTABLE = 6;
//删除
public static final int STATE_REMOVED = 7;
//移除完成
public static final int STATE_BAD_REMOVAL = 8;
/**
* 接受广播key
*/
public static final String ACTION_VOLUME_STATE_CHANGED = "android.os.storage.action.VOLUME_STATE_CHANGED";
public static final String EXTRA_VOLUME_ID = "android.os.storage.extra.VOLUME_ID";
public static final String EXTRA_VOLUME_STATE = "android.os.storage.extra.VOLUME_STATE";
/**
* 刚挂载和关闭
*/
public static final String ACTION_USB_DEVICE_ATTACHED= "android.hardware.usb.action.USB_DEVICE_ATTACHED";
public static final String ACTION_USB_DEVICE_DETACHED= "android.hardware.usb.action.USB_DEVICE_DETACHED";
}