博主信息
博文 143
粉丝 1
评论 0
访问量 425390
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Android监听U盘插拔广播,并获取路径和卷标名称
弘德誉曦的博客
原创
4221人浏览过

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举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学