博客列表 >Android监听U盘插拔广播,并获取路径和卷标名称

Android监听U盘插拔广播,并获取路径和卷标名称

弘德誉曦的博客
弘德誉曦的博客原创
2019年11月08日 17:29:543637浏览

Android监听U盘插拔广播,并获取路径和卷标名称

在xml下静态注册广播接收器

  1. <!-- 监听U盘插拔的广播-->
  2. <receiver android:name=".service.USBReceiver">
  3. <intent-filter android:priority="1000">
  4. <action android:name="android.intent.action.BOOT_COMPLETED"/>
  5. <action android:name="android.intent.action.MEDIA_MOUNTED"/>
  6. <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
  7. <action android:name="android.intent.action.MEDIA_REMOVED"/>
  8. <data android:scheme="file"></data>
  9. </intent-filter>
  10. </receiver>

使用下边的接收器获取

  1. public class USBReceiver extends BroadcastReceiver {
  2. private static final String TAG = USBReceiver.class.getSimpleName();
  3. private static final String MOUNTS_FILE = "/proc/mounts";
  4. private StorageManager mStorageManager;
  5. @Override
  6. public void onReceive(Context context, Intent intent) {
  7. mStorageManager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE);
  8. String action = intent.getAction();
  9. if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
  10. String mountPath = intent.getData().getPath();
  11. Uri data = intent.getData();
  12. Log.d(TAG, "mountPath = " + mountPath);
  13. if (!TextUtils.isEmpty(mountPath)) {
  14. //读取到U盘路径再做其他业务逻辑
  15. SPUtils.getInstance().put("UsbPath", mountPath);
  16. boolean mounted = isMounted(mountPath);
  17. Log.d(TAG, "onReceive: " + "U盘挂载" + mounted);
  18. getUName();
  19. }
  20. } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_EJECT)) {
  21. Log.d(TAG, "onReceive: " + "U盘移除了");
  22. } else if (action.equals("android.intent.action.BOOT_COMPLETED")) {
  23. //如果是开机完成,则需要调用另外的方法获取U盘的路径
  24. }
  25. }
  26. /**
  27. * 判断是否有U盘插入,当U盘开机之前插入使用该方法.
  28. * @param path
  29. * @return
  30. */
  31. public static boolean isMounted(String path) {
  32. boolean blnRet = false;
  33. String strLine = null;
  34. BufferedReader reader = null;
  35. try {
  36. reader = new BufferedReader(new FileReader(MOUNTS_FILE));
  37. while ((strLine = reader.readLine()) != null) {
  38. if (strLine.contains(path)) {
  39. blnRet = true;
  40. break;
  41. }
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. } finally {
  46. if (reader != null) {
  47. try {
  48. reader.close();
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. reader = null;
  53. }
  54. }
  55. return blnRet;
  56. }
  57. /**
  58. * 获取U盘的路径和名称
  59. */
  60. private void getUName() {
  61. Class<?> volumeInfoClazz = null;
  62. Method getDescriptionComparator = null;
  63. Method getBestVolumeDescription = null;
  64. Method getVolumes = null;
  65. Method isMountedReadable = null;
  66. Method getType = null;
  67. Method getPath = null;
  68. List<?> volumes = null;
  69. try {
  70. volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");
  71. getDescriptionComparator = volumeInfoClazz.getMethod("getDescriptionComparator");
  72. getBestVolumeDescription = StorageManager.class.getMethod("getBestVolumeDescription", volumeInfoClazz);
  73. getVolumes = StorageManager.class.getMethod("getVolumes");
  74. isMountedReadable = volumeInfoClazz.getMethod("isMountedReadable");
  75. getType = volumeInfoClazz.getMethod("getType");
  76. getPath = volumeInfoClazz.getMethod("getPath");
  77. volumes = (List<?>) getVolumes.invoke(mStorageManager);
  78. for (Object vol : volumes) {
  79. if (vol != null && (boolean) isMountedReadable.invoke(vol) && (int) getType.invoke(vol) == 0) {
  80. File path2 = (File) getPath.invoke(vol);
  81. String p1 = (String) getBestVolumeDescription.invoke(mStorageManager, vol);
  82. String p2 = path2.getPath();
  83. Log.d(TAG, "-----------path1-----------------" + p1); //打印U盘卷标名称
  84. Log.d(TAG, "-----------path2 @@@@@-----------------" + p2); //打印U盘路径
  85. }
  86. }
  87. } catch (Exception ex) {
  88. ex.printStackTrace();
  89. }
  90. }
  91. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议