A method for obtaining multi-channel USB mounting status and its path
By customizing tool classes and data classes in the Android system and monitoring the status of USB devices in combination with the system broadcasting, the function of obtaining the status of USB interfaces and mounting paths in real time is solved, and the problem that the existing technology cannot monitor the status of USB devices in real time is solved.
Patent Information
- Application Number
- CN202210181860.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-25
- Publication Date
- 2025-04-25
- Estimated Expiration
- 2042-02-25
AI Technical Summary
The existing message monitoring method cannot monitor the status of the USB device in real time, and cannot accurately know the unplugged/plugged status of a specific USB interface. If the USB device has been inserted before registering the monitoring event, the system broadcasting is not sticky, and event notifications may not be received after registration.
By declaring permissions to read and write USB disks in the global configuration file, the custom data class stores the volume label information of the USB device in the CommonUtil class, and customizes the tool class according to the preset query policy to perform data queries. Register the broadcast of the monitoring system, instantiate the broadcast receiving object, judge and process USB disk events based on broadcast events and tool classes, and realize the function of obtaining the USB interface status and mount path in real time.
Real-time monitoring of the USB device status is realized, accurately knowing the unplugged/plugged status of a certain USB interface, and obtaining the mounting path of the corresponding USB port, solving the problem that the original technology cannot monitor the USB device status in real time.
Smart Images

Figure CN114661530B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer program development, and in particular to a method for acquiring multi-channel USB mounting states and paths thereof. Background Art
[0002] Currently, the car computer generally has 2 to 3 USB ports. Project developers need to know whether a USB port is mounted with a USB flash drive, the plug / unplug status of the USB flash drive, and obtain the mount path of each USB flash drive in order to read files in the USB flash drive.
[0003] However, the native Android registration / unregistration method for monitoring USB device status broadcasts has the following defects:
[0004] ① Any USB device plugging in or out will trigger an event, so it is impossible to know the plugging / unplugging status of a specific USB interface;
[0005] ② If the USB device has been inserted before registering the monitoring event, and the system broadcast is not a sticky event, you will not receive the event notification after registration. Summary of the invention
[0006] The invention provides a method for acquiring multi-channel USB mounting status and paths thereof, which solves the technical problem that the existing message monitoring method cannot monitor the USB device status in real time.
[0007] In order to solve the above technical problems, the present invention provides a method for obtaining multi-channel USB mounting status and its path, comprising the steps of:
[0008] S1. Declare the permission to read and write USB flash drives in the global configuration file;
[0009] S2. Customize a data class in the CommonUtil class to store the volume label information of the USB device read from the StorageManager class;
[0010] S3. Customize at least one tool class in the CommonUtil class according to the preset query strategy to perform data query;
[0011] S4, register to monitor the system broadcast and monitor USB device related events;
[0012] S5. Instantiate a broadcast receiving object, and determine and process a corresponding USB flash drive event according to the broadcast event received from the monitoring system broadcast and the tool class.
[0013] In a further embodiment, step S3 comprises the steps of:
[0014] S31, customizing a first method in the CommonUtil class to obtain volume label information of each USB device, storing the information in the data class and returning the information to the caller;
[0015] S32, customizing the second method in the CommonUtil class to detect the mounting status of the USB device;
[0016] S33. Customize the third method in the CommonUtil class to obtain the USB path of the USB device.
[0017] In a further embodiment, step S31 comprises:
[0018] A. Get the StorageManager service proxy through SystemService;
[0019] B. Using the reflection mechanism, the getVolumes method in the StorageManager is called to obtain the volume label information of each USB device.
[0020] C. Use the Class class to obtain the VolumeInfo class by passing the parameter string;
[0021] D. Get the attribute object of the volume label of each USB device through the VolumeInfo class;
[0022] E. extracting the volume label information of the USB device from the attribute object of each USB device;
[0023] F. Save the volume label information in the data list of the data class and return it to the caller as a return value.
[0024] In a further implementation scheme, in the step S32, the detection of the mounting status of the USB device is specifically: traversing the data list called from the data class, and determining whether to mount the target USB device according to the parameter value.
[0025] In a further implementation scheme, in the step S33, the USB path of the USB device is obtained specifically by traversing the data list called from the data class, and determining whether to mount the target USB device according to the passed parameter value, and if the target USB device is mounted, returning the mounted path; otherwise returning a null value.
[0026] In a further implementation scheme, the step S5 is specifically as follows: instantiating a broadcast receiving object, receiving a broadcast event issued by the system, and parsing the intent parameter in the callback method to determine the type of the received USB flash drive event; and then calling the corresponding second method or the third method according to the type of USB flash drive event to perform data query, and performing judgment and processing based on the returned data.
[0027] In a further implementation, the step S4 further includes: when there is no need to monitor the USB device related events, deregistering the monitoring instance according to a deregistration method to cancel monitoring of the USB device related events.
[0028] In a further implementation scheme, the global configuration file is an AndroidManifest.xml file.
[0029] In a further implementation scheme, the volume label information includes a USB label, a USB path, a USB mounting status, and a serial number of the USB device.
[0030] In a further implementation scheme, the USB device related events include a USB disk access action, a USB disk removal action, and a USB disk mounting action.
[0031] The present invention sets a preset query strategy, and customizes the first method to the third method in the CommonUtil class. The volume label information of each USB device is automatically obtained through the first method, and saved in a data list of a preset data class. In this way, the data list is called by the second method and the third method to perform traversal query, and information such as whether the U disk is mounted and the corresponding mounting path can be obtained respectively; in this way, the broadcast method of monitoring the status of USB devices natively by Android is combined with the CommonUtil tool class to achieve the effect of obtaining the plug / unplug status of a certain USB interface in real time, and the mounting path of the corresponding USB port can also be obtained. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Figure 1 The present invention provides a flowchart of a method for obtaining multiple USB mount states and paths thereof. DETAILED DESCRIPTION
[0033] The following specifically illustrates the implementation mode of the present invention in conjunction with the accompanying drawings. The embodiments are provided for illustrative purposes only and are not to be construed as limitations of the present invention. The accompanying drawings are provided for reference and illustration only and do not constitute limitations on the scope of patent protection of the present invention, because many changes may be made to the present invention without departing from the spirit and scope of the present invention.
[0034] The embodiment of the present invention provides a method for obtaining the status of multiple USB mounts and their paths, such as Figure 1 As shown, in this embodiment, steps S1 to S5 are included:
[0035] S1. Declare the permission to read and write USB flash drives in the global configuration file;
[0036] In this embodiment, the global configuration file is the AndroidManifest.xml file. Android permissions are configured in the AndroidManifest.xml file. When a permission is required, it must be declared in the manifest file. <uses-permission>.
[0037] S2. Customize a data class in the CommonUtil class to store the volume label information of the USB device read from the StorageManager class;
[0038] In this embodiment, the volume label information includes but is not limited to a USB label, a USB path, a USB mounting status, and a serial number of a USB device.
[0039] StorageManager is a class in the Android SDK that manages storage devices, including internal storage and external storage. External storage can be peripherals such as SD cards and USB flash drives.
[0040] S3. Customize at least one tool class in the CommonUtil class according to the preset query strategy to perform data query, including steps S31 to S33:
[0041] S31, customizing the first method in the CommonUtil class to obtain the volume label information of each USB device, store it in the data class and return it to the caller, including steps A to F:
[0042] A. Get the StorageManager service proxy through SystemService;
[0043] B. Through StorageManager, using the reflection mechanism, call the getVolumes method in StorageManager to obtain the volume label information of each USB device;
[0044] C. Use the Class class to obtain the VolumeInfo class by passing the parameter string;
[0045] D. Get the attribute object of the volume label of each USB device through the VolumeInfo class;
[0046] E. Extract the volume label information of the USB device from the attribute (Field) object of each USB device;
[0047] F. Save the volume label information in the data list of the data class and return it to the caller as the return value.
[0048] S32, customizing the second method in the CommonUtil class to detect the mounting status of the USB device;
[0049] The specific method of detecting the mounting status of the USB device is as follows: traversing the data list called from the data class, and determining whether to mount the target USB device according to the parameter value.
[0050] S33. Customize the third method in the CommonUtil class to obtain the USB path of the USB device.
[0051] The specific steps of obtaining the USB path of the USB device are as follows: traverse the data list called from the data class, determine whether to mount the target USB device according to the parameter value, and if the target USB device is mounted, return the mounted path; otherwise, return null.
[0052] S4. During the life cycle of the component of the application (APP or service) that depends on the USB device, register the monitoring system broadcast to monitor USB device related events, and when it is no longer necessary to monitor USB device related events, unregister the monitoring instance according to the unregistration method to cancel monitoring of USB device related events.
[0053] In this embodiment, USB device related events include but are not limited to USB disk access action, USB disk removal action, and USB disk mounting action.
[0054] S5. Instantiate the broadcast receiving object, and judge and process the corresponding USB disk event according to the broadcast event and tool class received from the monitoring system broadcast, specifically:
[0055] Instantiate the broadcast receiving object, receive the broadcast event issued by the system, and parse the intent parameter in the callback method to determine the type of USB flash drive event received; then call the corresponding second method or third method to query the data according to the type of USB flash drive event, and make judgments and processes based on the returned data.
[0056] For example, when the USB flash drive event type is determined to be a USB flash drive plug-in event, the second method in the CommonUtil class is called (step S32) to detect whether the USB flash drive is mounted, and the mounting status of the corresponding target USB device is determined in the form of parameter transmission.
[0057] The embodiment of the present invention sets a preset query strategy, and customizes the first method to the third method in the CommonUtil class. The volume label information of each USB device is automatically obtained through the first method, and saved in a data list of a preset data class. In this way, the data list is called by the second method and the third method to traverse and query, and information such as whether the U disk is mounted and the corresponding mount path can be obtained respectively; in this way, the Android native broadcast method for monitoring the USB device status is combined with the CommonUtil tool class to achieve the effect of obtaining the plug / unplug status of a certain USB interface in real time, and the mount path of the corresponding USB port can also be obtained.
[0058] The above embodiments are preferred implementation modes of the present invention, but the implementation modes of the present invention are not limited to the above embodiments. Any other changes, modifications, substitutions, combinations, and simplifications that do not deviate from the spirit and principles of the present invention should be equivalent replacement methods and are included in the protection scope of the present invention.
Claims
1. A method for obtaining multi-channel USB mounting status and paths thereof, characterized in that: Includes steps: S1. Declare the permission to read and write USB flash drives in the global configuration file; S2. Customize a data class in the CommonUtil class to store the volume label information of the USB device read from the StorageManager class; S3. Customize at least one tool class in the CommonUtil class according to the preset query strategy to perform data query; S4, register to monitor the system broadcast and monitor USB device related events; S5, instantiate a broadcast receiving object, and determine and process the corresponding USB disk event according to the broadcast event received from the monitoring system broadcast and the tool class; The step S3 comprises the steps of: S31, customizing a first method in the CommonUtil class to obtain volume label information of each USB device, storing the information in the data class and returning the information to the caller; S32, customizing the second method in the CommonUtil class to detect the mounting status of the USB device; S33, customizing a third method in the CommonUtil class to obtain a USB path of the USB device; The step S31 comprises: A. Get the StorageManager service proxy through SystemService; B. Using the reflection mechanism, the getVolumes method in the StorageManager is called to obtain the volume label information of each USB device. C. Use the Class class to obtain the VolumeInfo class by passing the parameter string; D. Get the attribute object of the volume label of each USB device through the VolumeInfo class; E. extracting the volume label information of the USB device from the attribute object of each USB device; F. Save the volume label information in the data list of the data class and return it to the caller as a return value.
2. A method for obtaining multi-channel USB mounting status and paths thereof as claimed in claim 1, characterized in that: In the step S32, the detection of the mounting status of the USB device is specifically: traversing the data list called from the data class, and determining whether to mount the target USB device according to the parameter value.
3. A method for obtaining multi-channel USB mounting status and paths thereof as claimed in claim 1, characterized in that: In the step S33, the USB path of the USB device is obtained by traversing the data list called from the data class, judging whether to mount the target USB device according to the parameter value, and if the target USB device is mounted, returning the mounted path; otherwise, returning a null value.
4. A method for obtaining multi-channel USB mounting status and paths thereof as claimed in claim 1, characterized in that: The step S5 specifically includes: instantiating a broadcast receiving object, receiving a broadcast event issued by the system, and parsing the intent parameter in the callback method to determine the type of the received USB flash drive event; then calling the corresponding second method or the third method to perform data query according to the USB flash drive event type, and performing judgment and processing based on the returned data.
5. A method for acquiring multiple USB mounting states and paths thereof as claimed in claim 1, characterized in that: The step S4 further includes: when it is not necessary to monitor the USB device related events, deregistering the monitoring instance according to the deregistration method to cancel monitoring the USB device related events.
6. A method for obtaining multi-channel USB mounting status and paths thereof as claimed in claim 1, characterized in that: The global configuration file is the AndroidManifest.xml file.
7. A method for obtaining multiple USB mounting states and paths thereof as claimed in claim 1, characterized in that: The volume label information includes a USB label, a USB path, a USB mount status, and a serial number of a USB device.
8. A method for acquiring multiple USB mounting states and paths thereof as claimed in claim 5, characterized in that: The USB device related events include U disk access action, U disk removal action, and U disk mounting action.
Citation Information
Patent Citations
Virtual storage card configuration method and system
CN105100901A
Display method and display device of disk volume label
CN105307018A