1,在设置界面点击>存储->格式化内部存储将会执行
packages/apps/Settings/src/com/android/settings/deviceinfo/Memory.java
- if (preference == mSdFormat) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setClass(this, com.android.settings.MediaFormat.class);
- startActivity(intent);
- return true;
- }
2,packages/apps/Settings/src/com/android/settings/MediaFormat.java
下面将会跳到onCreate方法的establishInitialState();
- protected void onCreate(Bundle savedState) {
- @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); establishInitialState(); }
3,构造第一个含有一个按钮(格式化内部大容量存储设备)的页面,并为按钮添加事件
- private void establishInitialState() {
- if (mInitialView == null) {
- mInitialView = mInflater.inflate(R.layout.media_format_primary, null);
- mInitiateButton =
- (Button) mInitialView.findViewById(R.id.initiate_media_format);
- mInitiateButton.setOnClickListener(mInitiateListener);
- }
- }
4,当点击这个按钮时将会跳到确认页面,防止误操作
- private Button.OnClickListener mInitiateListener = new Button.OnClickListener() {
- public void onClick(View v) {
- if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
- establishFinalConfirmationState();
- }
- }
- };
5,确认格式化大容量存储设备的时候将会发送一个Intent,并将要格式化的路径传入
- Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
- intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
- String strPath = Environment.getExternalStorageDirectory().getPath();
- intent.putExtra("path", strPath);
- startService(intent);
6,frameworks/base/core/java/com/android/internal/os/storage/ExternalStorageFormatter.java
执行ExternalStorageFormatter类中的public int onStartCommand(Intent intent, int flags, int startId)方法接收路径并执行updateProgressState()方法
7,在updateProgressState()方法中判断路径是否存在,将卸载存储卡,
- if (Environment.MEDIA_MOUNTED.equals(status)
- || Environment.MEDIA_MOUNTED_READ_ONLY.equals(status)) {
- updateProgressDialog(mIsSdcard? R.string.progress_unmounting : R.string.progress_unmounting_nand);
- SystemProperties.set("sys.storage.state","formating");
- IMountService mountService = getMountService();
- try {
- /*
- * For mount point of external sdcard is: /mnt/sdcard/external_sd,
- * unmount sdcard first.
- */
- if (mPath.equals(Environment.getFlashStorageDirectory().toString())) {
- updateProgressDialog(R.string.progress_unmounting_nand);
- mountService.unmountVolume(Environment.getActExternalStorageDirectory().toString(),true);
- }
- mountService.unmountVolume(mPath, true);
- }
8,成功卸载SD卡会激发StorageEventListener mStorageListener = new StorageEventListener()事件
mStorageListener会重新执行updateProgressState()方法
9,再次执行updateProgressState()方法的时候SD卡已经卸载将会执行格式化操作
- if (Environment.MEDIA_NOFS.equals(status)
- || Environment.MEDIA_UNMOUNTED.equals(status)
- || Environment.MEDIA_UNMOUNTABLE.equals(status)) {
- updateProgressDialog(mIsSdcard? R.string.progress_erasing : R.string.progress_erasing_nand);
- SystemProperties.set("sys.storage.state","ready");
- final IMountService mountService = getMountService();
- final String extStoragePath = mPath;//Environment.getExternalStorageDirectory().toString();
- if (mountService != null) {
- new Thread() {
- public void run() {
- boolean success = false;
- try {
- mountService.formatVolume(extStoragePath);
- success = true;
- } catch (Exception e) {
- Toast.makeText(ExternalStorageFormatter.this, mIsSdcard?
- R.string.format_error : R.string.format_error_nand, Toast.LENGTH_LONG).show();
- }
- if (success) {
- if (mFactoryReset) {
- android.os.SystemProperties.set("ctl.start","bootanim");
- sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
- // Intent handling is asynchronous -- assume it will happen soon.
- stopSelf();
- return;
- }
- }
- // If we didn't succeed, or aren't doing a full factory
- // reset, then it is time to remount the storage.
- if (!success && mAlwaysReset) {
- sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
- }
10,success等于真的话会会清楚缓存,语句mountService.formatVolume(extStoragePath);是格式化操作
- public int formatVolume(String mountPoint) throws RemoteException {
- Parcel _data = Parcel.obtain();
- Parcel _reply = Parcel.obtain();
- int _result;
- try {
- _data.writeInterfaceToken(DESCRIPTOR);
- _data.writeString(mountPoint);
- mRemote.transact(Stub.TRANSACTION_formatVolume, _data, _reply, 0);
- _reply.readException();
- _result = _reply.readInt();
- } finally {
- _reply.recycle();
- _data.recycle();
- }
- return _result;
- }
本文出自 “背着重重壳的蜗牛” 博客,请务必保留此出处http://zchengdong.blog.51cto.com/3363091/845863
如果需要自己格式化某一个指定SD卡路径,如下:
Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY);
intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
//String strPath = Environment.getExternalStorageDirectory().getPath();
intent.putExtra("path", extStoragePath);
Bundle bundle = new Bundle();
bundle.putParcelable(StorageVolume.EXTRA_STORAGE_VOLUME,getDvrVolume());
intent.putExtras(bundle);
startService(intent);
public StorageVolume getDvrVolume() {
return getPrimaryVolume(getVolumeList());
}
/** {@hide} */
public static StorageVolume getPrimaryVolume(StorageVolume[] volumes) {
for (StorageVolume volume : volumes) {
if (volume.getPath().equals("/mnt/extsd2")) {
Log.w(TAG, "volume is :"+volume.getPath());
return volume;
}
}
Log.w(TAG, "No primary storage defined");
return null;
}