存储设置
一:onCreate()方法
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
final Context context = getActivity();
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);//获得USB服务
mStorageManager = StorageManager.from(context);
mStorageManager.registerListener(mStorageListener);//存储管理者
addPreferencesFromResource(R.xml.device_info_memory);//加载布局
addCategory(StorageVolumePreferenceCategory.buildForInternal(context));
final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
for (StorageVolume volume : storageVolumes) {
// add those storage volumes which are not emulated & allow UMS.
// sometimes a storage drive like Mega SIM could carry two volumes,
// with only one volume supported for UMS.
if (!volume.isEmulated()) {
// check if the colume is for UICC
if (MediaFormat.isUiccStorage(volume, context) /*&& !volume.allowMassStorage()*/) {
// UICC volume that is not UMS enabled, shouldn't be shown on UI
Log.w(TAG, "UICC volume not UMS enabled. Not showing up on UI");
continue;
}
if(!volume.allowMassStorage()){
continue;
}
addCategory(StorageVolumePreferenceCategory.buildForPhysical(context, volume));
}
}
setHasOptionsMenu(true);
}
二:看XML文件<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/storage_settings_title">
<!-- Preference categories are dynamically created based on the list of available storage volumes -->
</PreferenceScreen>
三:分析<pre name="code" class="java"> addCategory(StorageVolumePreferenceCategory.buildForInternal(context));这个方法添加到fragment
private void addCategory(StorageVolumePreferenceCategory category) {
mCategories.add(category);///是个集合添加到这个集合中
getPreferenceScreen().addPreference(category);//每个prefrence中添加
category.init();初始化在StorageVolumePreferenceCategory这类中
}
StorageVolumePreferenceCategory这个类:来初始化每个category