Android中通过反射的方式判断U盘是否真正挂载

2024-09-05 07:58

本文主要是介绍Android中通过反射的方式判断U盘是否真正挂载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

       由于StorageManager.java类中的getVolumeList()和getVolumeState(String mountPoint)方法是hide(隐藏)的,所以需要通过反射的方式获取对应的存储信息。源码./frameworks/base/core/java/android/os/storage/StorageManager.java类中的getVolumeList()跟getVolumeState(String mountPoint)方法如下:

/*** Returns list of all mountable volumes.* @hide*/public StorageVolume[] getVolumeList() {if (mMountService == null) return new StorageVolume[0];try {Parcelable[] list = mMountService.getVolumeList();if (list == null) return new StorageVolume[0];int length = list.length;StorageVolume[] result = new StorageVolume[length];for (int i = 0; i < length; i++) {result[i] = (StorageVolume)list[i];}return result;} catch (RemoteException e) {Log.e(TAG, "Failed to get volume list", e);return null;}}/*** Gets the state of a volume via its mountpoint.* @hide*/public String getVolumeState(String mountPoint) {if (mMountService == null) return Environment.MEDIA_REMOVED;try {return mMountService.getVolumeState(mountPoint);} catch (RemoteException e) {Log.e(TAG, "Failed to get volume state", e);return null;}}

可以通过反射的方式获取 getVolumeList()跟getVolumeState(String mountPoint)的方法,实现如下:

    /*** Returns list of all mountable volumes.*/public static StorageVolume[] getVolumeList(StorageManager storageManager){try {Class clz = StorageManager.class;Method getVolumeList = clz.getMethod("getVolumeList", null);StorageVolume[] result = (StorageVolume[]) getVolumeList.invoke(storageManager, null);return result;} catch (Exception e) {e.printStackTrace();}return null;}/*** Gets the state of a volume via its mountpoint.*/public static String getVolumeState(StorageManager storageManager, String path){String result = "";if(null == storageManager || TextUtils.isEmpty(path)){return result;}try {Class clz = StorageManager.class;Method getVolumeList = clz.getMethod("getVolumeState", String.class);result = (String) getVolumeList.invoke(storageManager, path);} catch (Exception e) {e.printStackTrace();}return result;}

 为了方便使用,把这两个方法封装到了USBUtil.java类中,具体实现如下:

package com.example.helloworld.util;import java.lang.reflect.Method;import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.text.TextUtils;/*** Created by ryan on 01/03/2018.*/public class USBUtil {private static final USBUtil mUtil = new USBUtil();public static USBUtil getInstance() {return mUtil;}/*** Returns list of all mountable volumes.*/public static StorageVolume[] getVolumeList(StorageManager storageManager){try {Class clz = StorageManager.class;Method getVolumeList = clz.getMethod("getVolumeList", null);StorageVolume[] result = (StorageVolume[]) getVolumeList.invoke(storageManager, null);return result;} catch (Exception e) {e.printStackTrace();}return null;}/*** Gets the state of a volume via its mountpoint.*/public static String getVolumeState(StorageManager storageManager, String path){String result = "";if(null == storageManager || TextUtils.isEmpty(path)){return result;}try {Class clz = StorageManager.class;Method getVolumeList = clz.getMethod("getVolumeState", String.class);result = (String) getVolumeList.invoke(storageManager, path);} catch (Exception e) {e.printStackTrace();}return result;}}

接下来是方法的使用,通过UsbTest.java类来进行界面测试,具体实现如下:

package com.example.helloworld;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.view.View;
import android.widget.TextView;import com.example.helloworld.util.Logger;
import com.example.helloworld.util.USBUtil;public class UsbTest extends Activity{private StorageManager storageManager;Context context;TextView mTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.usb);context = UsbTest.this;storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); //存储服务初始化mTextView = (TextView) findViewById(R.id.show_tv);}public void startVoice(View view){Logger.d();if (USBExist(context)){mTextView.setText("USB is exist!");}else {mTextView.setText("USB isn't exist!");}}public void finishVoice(View view){Logger.d();finish();}//判断/storage/udisk是否为挂载的路径private boolean USBExist(Context context){if (null == storageManager){Logger.d("Invalid reference to StorageManager received.");return false;}String usbPath = getUSBPath(context);if (USBUtil.getVolumeState(storageManager, usbPath).equals(android.os.Environment.MEDIA_MOUNTED)){return true;}return false;}//判断USB路径(/storage/udisk)是否存在private String getUSBPath(Context context){Logger.d();String usb = null;StorageVolume[] volumes = USBUtil.getVolumeList(storageManager);for (int i = 0; i < volumes.length; i++){if (volumes[i].isRemovable() && volumes[i].getDescription(context).contains("USB")){usb = volumes[i].getPath();Logger.d("usb = " + usb);if (usb.equals("/storage/udisk")){break;}}}return usb;}}

usb.xml布局如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/show_tv"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="startVoice"android:text="@string/usb_check"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="finishVoice"android:text="@string/version_quit"/></LinearLayout>

 测试结果如下:

a.未插U盘

b.插了U盘

 

 代码参考:https://github.com/gunder1129/android-tool/tree/master/AIDLdemo/SimpleJarClient

注意:由于用到源码中的./frameworks/base/core/java/android/os/storage/StorageManager.java类,所以需要Android的系统平台进行编译。

 

 

 

 

 

 

这篇关于Android中通过反射的方式判断U盘是否真正挂载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1138378

相关文章

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

JS 实现复制到剪贴板的几种方式小结

《JS实现复制到剪贴板的几种方式小结》本文主要介绍了JS实现复制到剪贴板的几种方式小结,包括ClipboardAPI和document.execCommand这两种方法,具有一定的参考价值,感兴趣的... 目录一、Clipboard API相关属性方法二、document.execCommand优点:缺点:

Python创建Excel的4种方式小结

《Python创建Excel的4种方式小结》这篇文章主要为大家详细介绍了Python中创建Excel的4种常见方式,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以学习一下... 目录库的安装代码1——pandas代码2——openpyxl代码3——xlsxwriterwww.cppcns.c