-
第一步现在xml下静态注册广播接收器
<!-- 监听U盘插拔的广播--> <receiver android:name=".service.USBReceiver"> <intent-filter android:priority="1000"> <action android:name="android.intent.action.BOOT_COMPLETED"/> <action android:name="android.intent.action.MEDIA_MOUNTED"/> <action android:name="android.intent.action.MEDIA_UNMOUNTED" /> <action android:name="android.intent.action.MEDIA_REMOVED"/> <data android:scheme="file"></data> </intent-filter> </receiver>
-
第二步使用下边的接收器获取
public class USBReceiver extends BroadcastReceiver { private static final String TAG = USBReceiver.class.getSimpleName(); private static final String MOUNTS_FILE = "/proc/mounts"; private StorageManager mStorageManager; @Override public void onReceive(Context context, Intent intent) { mStorageManager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE); String action = intent.getAction(); if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) { String mountPath = intent.getData().getPath(); Uri data = intent.getData(); Log.d(TAG, "mountPath = " + mountPath); if (!TextUtils.isEmpty(mountPath)) { //读取到U盘路径再做其他业务逻辑 SPUtils.getInstance().put("UsbPath", mountPath); boolean mounted = isMounted(mountPath); Log.d(TAG, "onReceive: " + "U盘挂载" + mounted); getUName(); } } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_EJECT)) { Log.d(TAG, "onReceive: " + "U盘移除了"); } else if (action.equals("android.intent.action.BOOT_COMPLETED")) { //如果是开机完成,则需要调用另外的方法获取U盘的路径 } } /** * 判断是否有U盘插入,当U盘开机之前插入使用该方法. * @param path * @return */ public static boolean isMounted(String path) { boolean blnRet = false; String strLine = null; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(MOUNTS_FILE)); while ((strLine = reader.readLine()) != null) { if (strLine.contains(path)) { blnRet = true; break; } } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } reader = null; } } return blnRet; } /** * 获取U盘的路径和名称 */ private void getUName() { Class<?> volumeInfoClazz = null; Method getDescriptionComparator = null; Method getBestVolumeDescription = null; Method getVolumes = null; Method isMountedReadable = null; Method getType = null; Method getPath = null; List<?> volumes = null; try { volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo"); getDescriptionComparator = volumeInfoClazz.getMethod("getDescriptionComparator"); getBestVolumeDescription = StorageManager.class.getMethod("getBestVolumeDescription", volumeInfoClazz); getVolumes = StorageManager.class.getMethod("getVolumes"); isMountedReadable = volumeInfoClazz.getMethod("isMountedReadable"); getType = volumeInfoClazz.getMethod("getType"); getPath = volumeInfoClazz.getMethod("getPath"); volumes = (List<?>) getVolumes.invoke(mStorageManager); for (Object vol : volumes) { if (vol != null && (boolean) isMountedReadable.invoke(vol) && (int) getType.invoke(vol) == 0) { File path2 = (File) getPath.invoke(vol); String p1 = (String) getBestVolumeDescription.invoke(mStorageManager, vol); String p2 = path2.getPath(); Log.d(TAG, "-----------path1-----------------" + p1); //打印U盘卷标名称 Log.d(TAG, "-----------path2 @@@@@-----------------" + p2); //打印U盘路径 } } } catch (Exception ex) { ex.printStackTrace(); } } }
Android监听U盘插拔广播,并获取路径和卷标名称
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Correctness AdapterViewChildren Summary: AdapterViews can...
- 1、不安全的随机数生成,在CSRF TOKEN生成、password reset token生成等,会造成toke...