[Android Framework]獲取U盤 SD 狀態
Android 4.4 SD 和 U盤 的狀態
通過獲取StorageVolume 類來判斷 是sd還是U盤。我們來看一下這個類
package android.os.storage;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import com.android.internal.util.IndentingPrintWriter;
import java.io.CharArrayWriter;
import java.io.File;
/**
* Description of a storage volume and its capabilities, including the
* filesystem path where it may be mounted.
*
* @hide
*/
public class StorageVolume implements Parcelable {
// TODO: switch to more durable token
private int mStorageId;
private final File mPath;
private final int mDescriptionId;
private final boolean mPrimary;
private final boolean mRemovable;
private final boolean mEmulated;
private final int mMtpReserveSpace;
private final boolean mAllowMassStorage;
/** Maximum file size for the storage, or zero for no limit */
private final long mMaxFileSize;
/** When set, indicates exclusive ownership of this volume */
private final UserHandle mOwner;
private String mUuid;
private String mUserLabel;
private String mState;
// StorageVolume extra for ACTION_MEDIA_REMOVED, ACTION_MEDIA_UNMOUNTED, ACTION_MEDIA_CHECKING,
// ACTION_MEDIA_NOFS, ACTION_MEDIA_MOUNTED, ACTION_MEDIA_SHARED, ACTION_MEDIA_UNSHARED,
// ACTION_MEDIA_BAD_REMOVAL, ACTION_MEDIA_UNMOUNTABLE and ACTION_MEDIA_EJECT broadcasts.
public static final String EXTRA_STORAGE_VOLUME = "storage_volume";
// About storage volume format fs type
public static final String EXTRA_FORMAT_FSTYPE = "format_fstype";
public StorageVolume(File path, int descriptionId, boolean primary, boolean removable,
boolean emulated, int mtpReserveSpace, boolean allowMassStorage, long maxFileSize,
UserHandle owner) {
mPath = path;
mDescriptionId = descriptionId;
mPrimary = primary;
mRemovable = removable;
mEmulated = emulated;
mMtpReserveSpace = mtpReserveSpace;
mAllowMassStorage = allowMassStorage;
mMaxFileSize = maxFileSize;
mOwner = owner;
}
private StorageVolume(Parcel in) {
mStorageId = in.readInt();
mPath = new File(in.readString());
mDescriptionId = in.readInt();
mPrimary = in.readInt() != 0;
mRemovable = in.readInt() != 0;
mEmulated = in.readInt() != 0;
mMtpReserveSpace = in.readInt();
mAllowMassStorage = in.readInt() != 0;
mMaxFileSize = in.readLong();
mOwner = in.readParcelable(null);
mUuid = in.readString();
mUserLabel = in.readString();
mState = in.readString();
}
public static StorageVolume fromTemplate(StorageVolume template, File path, UserHandle owner) {
return new StorageVolume(path, template.mDescriptionId, template.mPrimary,
template.mRemovable, template.mEmulated, template.mMtpReserveSpace,
template.mAllowMassStorage, template.mMaxFileSize, owner);
}
/**
* Returns the mount path for the volume.
*
* @return the mount path
*/
public String getPath() {
return mPath.toString();
}
public File getPathFile() {
return mPath;
}
/**
* Returns a user visible description of the volume.
*
* @return the volume description
*/
public String getDescription(Context context) {
return context.getResources().getString(mDescriptionId);
}
public int getDescriptionId() {
return mDescriptionId;
}
public boolean isPrimary() {
return mPrimary;
}
/**
* Returns true if the volume is removable.
*
* @return is removable
*/
public boolean isRemovable() {
return mRemovable;
}
/**
* Returns true if the volume is emulated.
*
* @return is removable
*/
public boolean isEmulated() {
return mEmulated;
}
/**
* Returns the MTP storage ID for the volume.
* this is also used for the storage_id column in the media provider.
*
* @return MTP storage ID
*/
public int getStorageId() {
return mStorageId;
}
/**
* Do not call this unless you are MountService
*/
public void setStorageId(int index) {
// storage ID is 0x00010001 for primary storage,
// then 0x00020001, 0x00030001, etc. for secondary storages
mStorageId = ((index + 1) << 16) + 1;
}
/**
* Number of megabytes of space to leave unallocated by MTP.
* MTP will subtract this value from the free space it reports back
* to the host via GetStorageInfo, and will not allow new files to
* be added via MTP if there is less than this amount left free in the storage.
* If MTP has dedicated storage this value should be zero, but if MTP is
* sharing storage with the rest of the system, set this to a positive value
* to ensure that MTP activity does not result in the storage being
* too close to full.
*
* @return MTP reserve space
*/
public int getMtpReserveSpace() {
return mMtpReserveSpace;
}
/**
* Returns true if this volume can be shared via USB mass storage.
*
* @return whether mass storage is allowed
*/
public boolean allowMassStorage() {
return mAllowMassStorage;
}
/**
* Returns maximum file size for the volume, or zero if it is unbounded.
*
* @return maximum file size
*/
public long getMaxFileSize() {
return mMaxFileSize;
}
public UserHandle getOwner() {
return mOwner;
}
public void setUuid(String uuid) {
mUuid = uuid;
}
public String getUuid() {
return mUuid;
}
/**
* Parse and return volume UUID as FAT volume ID, or return -1 if unable to
* parse or UUID is unknown.
*/
public int getFatVolumeId() {
if (mUuid == null || mUuid.length() != 9) {
return -1;
}
try {
return Integer.parseInt(mUuid.replace("-", ""), 16);
} catch (NumberFormatException e) {
return -1;
}
}
public void setUserLabel(String userLabel) {
mUserLabel = userLabel;
}
public String getUserLabel() {
return mUserLabel;
}
public void setState(String state) {
mState = state;
}
public String getState() {
return mState;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof StorageVolume && mPath != null) {
StorageVolume volume = (StorageVolume)obj;
return (mPath.equals(volume.mPath));
}
return false;
}
@Override
public int hashCode() {
return mPath.hashCode();
}
@Override
public String toString() {
final CharArrayWriter writer = new CharArrayWriter();
dump(new IndentingPrintWriter(writer, " ", 80));
return writer.toString();
}
public void dump(IndentingPrintWriter pw) {
pw.println("StorageVolume:");
pw.increaseIndent();
pw.printPair("mStorageId", mStorageId);
pw.printPair("mPath", mPath);
pw.printPair("mDescriptionId", mDescriptionId);
pw.printPair("mPrimary", mPrimary);
pw.printPair("mRemovable", mRemovable);
pw.printPair("mEmulated", mEmulated);
pw.printPair("mMtpReserveSpace", mMtpReserveSpace);
pw.printPair("mAllowMassStorage", mAllowMassStorage);
pw.printPair("mMaxFileSize", mMaxFileSize);
pw.printPair("mOwner", mOwner);
pw.printPair("mUuid", mUuid);
pw.printPair("mUserLabel", mUserLabel);
pw.printPair("mState", mState);
pw.decreaseIndent();
}
public static final Creator<StorageVolume> CREATOR = new Creator<StorageVolume>() {
@Override
public StorageVolume createFromParcel(Parcel in) {
return new StorageVolume(in);
}
@Override
public StorageVolume[] newArray(int size) {
return new StorageVolume[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(mStorageId);
parcel.writeString(mPath.toString());
parcel.writeInt(mDescriptionId);
parcel.writeInt(mPrimary ? 1 : 0);
parcel.writeInt(mRemovable ? 1 : 0);
parcel.writeInt(mEmulated ? 1 : 0);
parcel.writeInt(mMtpReserveSpace);
parcel.writeInt(mAllowMassStorage ? 1 : 0);
parcel.writeLong(mMaxFileSize);
parcel.writeParcelable(mOwner, flags);
parcel.writeString(mUuid);
parcel.writeString(mUserLabel);
parcel.writeString(mState);
}
}
這個類用來描述儲存狀態,有幾個重要的引數:
- File mPath 路徑
- mRemovable 是否可移除,(U盤,SD)返回 true
- getDescription(context) 獲取裝置的描述 "sd" 就是SD卡,“USB”就是usb裝置
獲取儲存裝置
mStorageManager = StorageManager.from(this);
final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
for (StorageVolume mVolume : storageVolumes) {
if(mVolume != null){
String description = mVolume.getDescription(context);
Log.d(getClass().getName(), "description is: " + description);
if(description != null && (description.contains("SD") || description.contains("sd"))){
//這是sd
}
else if(description != null && (description.contains("USB") || description.contains("usb")
|| description.contains("Usb"))){
//這是usb
}
}
}
}
儲存裝置狀態和路徑
import android.os.storage.StorageManager;
import android.os.storage.StorageEventListener;
private StorageManager mStorageManager;
mStorageManager = StorageManager.from(this);
//註冊和反註冊
mStorageManager.registerListener(mStorageListener);
if (mStorageManager != null && mStorageListener != null) {
mStorageManager.unregisterListener(mStorageListener);
}
/**
*
* 監聽 path 路徑 oldState 上一個狀態 newState 當前狀態
* 所有狀態 mounted unmounted removed checking
*/
StorageEventListener mStorageListener = new StorageEventListener() {
@Override
public void onStorageStateChanged(String path, String oldState, String newState) {
Log.i("qkmin -voolesettings"," StorageEventListener Received storage state changed notification that " + path +
" changed state from " + oldState + " to " + newState);
}
};
相關文章
- Android 監聽鍵盤狀態變化,並獲取鍵盤高度Android
- Android獲取狀態列高度Android
- 完美獲取Android狀態列高度Android
- [Android]獲取網路連線狀態Android
- Go 獲取已插入 U 盤碟符Go
- 固態u盤和普通u盤哪個好 固態u盤和普通u盤的區別
- 獲取Mysql的狀態、變數MySql變數
- Android APP如何獲取裝置網線插拔的狀態AndroidAPP
- android監聽SD卡掛載並獲取路徑AndroidSD卡
- [React Native]獲取網路狀態React Native
- Jquery獲取radio的狀態jQuery
- 理解alivc_framework狀態機Framework
- 微信小程式直播狀態介面如何獲取微信小程式
- redis 使用GETRANGE 來獲取一組bitmap狀態Redis
- DB2_獲取系統引數狀態DB2
- 獲取SD卡上所有的圖片SD卡
- 獲取SD卡序列號和廠商IDSD卡
- Android SD卡 路徑問題以及如何獲取SDCard 記憶體AndroidSD卡記憶體
- Django REST framework API 指南(25):狀態碼DjangoRESTFrameworkAPI
- 車載U盤和普通U盤哪個好 車載U盤和普通U盤的區別
- 直播平臺原始碼,快速獲取當前狀態列高度原始碼
- u盤電腦不識別瞭如何修復 u盤不能讀取的解決方法
- Android開發之監聽軟鍵盤狀態(彈出收回)Android
- 關於獲取android訊號強度和電池狀態變化圖示修改的方法Android
- js中獲取鍵盤事件JS事件
- 如何獲取Vivo系統的懸浮窗許可權狀態
- Android 狀態列透明Android
- android狀態列一體化(沉浸式狀態列)Android
- VMwareWorkstation設定U盤啟動(或U盤使用)
- 清除 Nuxt 狀態快取:clearNuxtStateUX快取
- Android--手持PDA讀取SD卡中檔案AndroidSD卡
- php動態獲取常量PHP
- React Native 跳轉到 APP 推送頁面並獲取推送狀態React NativeAPP
- HarmonyOS 如何獲取裝置資訊(系統、版本、網路連線狀態)
- 改造動態framework為靜態frameworkFramework
- Revit獲取元素的巢狀族巢狀
- Qt 介面獲取鍵盤Enter鍵QT
- android開發中,監聽軟鍵盤的狀態(收起或者開啟)Android