Broadcasting reception and USB flash disk broadcasting

Source: Internet
Author: User

Android storage device (USB flash drive, SD card) Status Monitoring
   

We are using a DV6300-T platform for testing, found there are two ways to detect the android external media (including SD card, USB) status.

Either StorageListener or broadcast.

 

DV6300-T storage equipment related analysis:

Related classes include:
RecordDeviceManager
DeviceStateListener ChoiceRecordDevice
The observer mode is used to monitor device plugging to trigger various situations:

For example, the observer mRecordDeviceListener is added to DTVLauncher, and the time shifting or recording will be stopped when the device is detected to be pulled out.

 

The first monitoring method:

Use StorageManager
IMountService
StorageEventListener class to control (you can refer to the source code of the DV6300-T ):

 

StorageManager mStorageManager =
(StorageManager) context. getSystemServic (Context. STORAGE_SERVICE );

MStorageManager. registerListener (mStorageListener );
IMountService mMountService =
IMountService. Stub. asInterface (ServiceManager. getService ("mount "));

StorageEventListener
MStorageListener = new StorageEventListener (){

@ Override
Public
Void onStorageStateChanged (String
Path, String oldState, String newState ){

If (path. equals (mRecordDeviceStorageName )){


Log. I ("usb", path + ":" + oldState + "--->" + "newState ");


If (newState. equals (Environment. MEDIA_UNMOUNTED ))
{

Yyobservers ();

}

}
}
};

We can determine the current status based on the three parameters in the onStorageStateChanged method, and determine whether it is an SD card (/mnt/sdcard) or a USB device (/mnt/sda) based on the path ).

For example, on the DV6300-T, we print the following:

Insert SD card:
OnStorageStateChanged will be called three times: the parameters are:
/Mnt/sdcard/extend_sd:
Removed ---> unmounted
/Mnt/sdcard/extend_sd: unmounted ---> checking
/Mnt/sdcard/extend_sd: checking ---> mounted

USB flash drive:
/Mnt/sda1
: Unmounted ---> checking
/Mnt/sda1: checking ---> mounted
Unplug the SD card:
/Mnt/sdcard/extend_sd: mounted ---> unmounted
/Mnt/sdcard/extend_sd: unmounted ---> removed

USB flash drive:
/Mnt/sda1: mounted ---> unmounted
/Mnt/sda1: unmounted ---> removed
/Mnt/sda1: removed ---> unmounted

 

 

2nd monitoring methods (broadcast methods ):

Class UsbReceiver {
Private BroadcastReceiver
MReceiver;
UsbReceiver (Context
Context ){

MReceiver = new BroadcastReceiver (){

@ Override

Public void onReceive (Context context, Intent
Intent ){


// Intent. getAction (); get the current status of the storage device


Log. I ("usb", "BroadcastReceiver:" + intent. getAction ());


// Intent. getData (). getPath (); get the path of the storage device

Log. I ("usb", "path:" + intent. getData (). getPath ());


}


};


IntentFilter filter = new IntentFilter ();

Filter. addAction (Intent. ACTION_MEDIA_SHARED); // return if the SDCard is not installed and shared by the USB bulk storage

Filter. addAction (Intent. ACTION_MEDIA_MOUNTED); // indicates that the sd object exists and has read/write permissions.

Filter. addAction (Intent. ACTION_MEDIA_UNMOUNTED); // The SDCard has been detached. If the SDCard exists but is not installed
Filter. addAction (Intent. ACTION_MEDIA_CHECKING );
// Indicates that the object is checking the disk
Filter. addAction (Intent. ACTION_MEDIA_EJECT );
// Physical unplugging
SDCARD
Filter. addAction (Intent. ACTION_MEDIA_REMOVED );
// Completely unplugging
Filter. addDataScheme ("file ");
//
This row must exist; otherwise, the broadcast cannot be received.
Context. registerReceiver (mReceiver,
Filter );
}
}

The intent. getData () passed through the broadcast will get a uri, and then uri. getPath () is the usb path to be inserted. You can record the usb path inserted or pulled each time,

For example, on the DV6300 platform:
The USB flash drive returns/mnt/sda1, while the SD card returns/mnt/sdcard/extend_sd.
GetAction obtains the current status, as described below:

USB flash drive insertion:
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED
Intent. getAction () = android. intent. action. MEDIA_CHECKING
Intent. getAction () = android. intent. action. MEDIA_MOUNTED
USB flash drive:
Intent. getAction () = android. intent. action. MEDIA_EJECT
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED
Intent. getAction () = android. intent. action. MEDIA_REMOVED
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED

SD card insertion:
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED
Intent. getAction () = android. intent. action. MEDIA_CHECKING
Intent. getAction () = android. intent. action. MEDIA_MOUNTED
SD card unplugging:
Intent. getAction () = android. intent. action. MEDIA_EJECT
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED
Intent. getAction () = android. intent. action. MEDIA_UNMOUNTED
Intent. getAction () = android. intent. action. MEDIA_REMOVED

 

 

 

Refer to blog:

Android monitoring usb plugging

The Android framework layer adds new interfaces for IMountService

Android usb Mount analysis --- start MountService

Android Binder Mechanism

Analysis of SD card mounting process in Android 2.3 (7)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.