Android 格式化内部存储

1,在设置界面点击>存储->格式化内部存储将会执行

packages/apps/Settings/src/com/android/settings/deviceinfo/Memory.java

  1. if(preference==mSdFormat){
  2. Intentintent=newIntent(Intent.ACTION_VIEW);
  3. intent.setClass(this,com.android.settings.MediaFormat.class);
  4. startActivity(intent);
  5. returntrue;
  6. }

2,packages/apps/Settings/src/com/android/settings/MediaFormat.java

下面将会跳到onCreate方法的establishInitialState();

            
  1. protectedvoidonCreate(BundlesavedState){
  2. @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); establishInitialState(); }


3,构造第一个含有一个按钮(格式化内部大容量存储设备)的页面,并为按钮添加事件

                
  1. privatevoidestablishInitialState(){
  2. if(mInitialView==null){
  3. mInitialView=mInflater.inflate(R.layout.media_format_primary,null);
  4. mInitiateButton=
  5. (Button)mInitialView.findViewById(R.id.initiate_media_format);
  6. mInitiateButton.setOnClickListener(mInitiateListener);
  7. }
  8. }

4,当点击这个按钮时将会跳到确认页面,防止误操作

                    
  1. privateButton.OnClickListenermInitiateListener=newButton.OnClickListener(){
  2. publicvoidonClick(Viewv){
  3. if(!runKeyguardConfirmation(KEYGUARD_REQUEST)){
  4. establishFinalConfirmationState();
  5. }
  6. }
  7. };

5,确认格式化大容量存储设备的时候将会发送一个Intent,并将要格式化的路径传入

                        
  1. Intentintent=newIntent(ExternalStorageFormatter.FORMAT_ONLY);
  2. intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
  3. StringstrPath=Environment.getExternalStorageDirectory().getPath();
  4. intent.putExtra("path",strPath);
  5. 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()方法中判断路径是否存在,将卸载存储卡,

                        
  1. if(Environment.MEDIA_MOUNTED.equals(status)
  2. ||Environment.MEDIA_MOUNTED_READ_ONLY.equals(status)){
  3. updateProgressDialog(mIsSdcard?R.string.progress_unmounting:R.string.progress_unmounting_nand);
  4. SystemProperties.set("sys.storage.state","formating");
  5. IMountServicemountService=getMountService();
  6. try{
  7. /*
  8. *Formountpointofexternalsdcardis:/mnt/sdcard/external_sd,
  9. *unmountsdcardfirst.
  10. */
  11. if(mPath.equals(Environment.getFlashStorageDirectory().toString())){
  12. updateProgressDialog(R.string.progress_unmounting_nand);
  13. mountService.unmountVolume(Environment.getActExternalStorageDirectory().toString(),true);
  14. }
  15. mountService.unmountVolume(mPath,true);
  16. }

8,成功卸载SD卡会激发StorageEventListener mStorageListener = new StorageEventListener()事件

mStorageListener会重新执行updateProgressState()方法

9,再次执行updateProgressState()方法的时候SD卡已经卸载将会执行格式化操作

                            
  1. if(Environment.MEDIA_NOFS.equals(status)
  2. ||Environment.MEDIA_UNMOUNTED.equals(status)
  3. ||Environment.MEDIA_UNMOUNTABLE.equals(status)){
  4. updateProgressDialog(mIsSdcard?R.string.progress_erasing:R.string.progress_erasing_nand);
  5. SystemProperties.set("sys.storage.state","ready");
  6. finalIMountServicemountService=getMountService();
  7. finalStringextStoragePath=mPath;//Environment.getExternalStorageDirectory().toString();
  8. if(mountService!=null){
  9. newThread(){
  10. publicvoidrun(){
  11. booleansuccess=false;
  12. try{
  13. mountService.formatVolume(extStoragePath);
  14. success=true;
  15. }catch(Exceptione){
  16. Toast.makeText(ExternalStorageFormatter.this,mIsSdcard?
  17. R.string.format_error:R.string.format_error_nand,Toast.LENGTH_LONG).show();
  18. }
  19. if(success){
  20. if(mFactoryReset){
  21. android.os.SystemProperties.set("ctl.start","bootanim");
  22. sendBroadcast(newIntent("android.intent.action.MASTER_CLEAR"));
  23. //Intenthandlingisasynchronous--assumeitwillhappensoon.
  24. stopSelf();
  25. return;
  26. }
  27. }
  28. //Ifwedidn'tsucceed,oraren'tdoingafullfactory
  29. //reset,thenitistimetoremountthestorage.
  30. if(!success&&mAlwaysReset){
  31. sendBroadcast(newIntent("android.intent.action.MASTER_CLEAR"));
  32. }

10,success等于真的话会会清楚缓存,语句mountService.formatVolume(extStoragePath);是格式化操作

                                
  1. publicintformatVolume(StringmountPoint)throwsRemoteException{
  2. Parcel_data=Parcel.obtain();
  3. Parcel_reply=Parcel.obtain();
  4. int_result;
  5. try{
  6. _data.writeInterfaceToken(DESCRIPTOR);
  7. _data.writeString(mountPoint);
  8. mRemote.transact(Stub.TRANSACTION_formatVolume,_data,_reply,0);
  9. _reply.readException();
  10. _result=_reply.readInt();
  11. }finally{
  12. _reply.recycle();
  13. _data.recycle();
  14. }
  15. return_result;
  16. }

更多相关文章

  1. 【Android】安卓中常用的图片加载方法
  2. 如何让Android横竖屏切换时不销毁当前activity
  3. Android(安卓)6.0 Ethernet流程分析记录
  4. android configChanges
  5. android 申请移动应用的签名生成方法
  6. Android(安卓)性能优化的一些方法
  7. Android(安卓)SDK下载和更新失败的解决方法
  8. AgentWeb WebView 与 Android交互 JS调用 Android
  9. android ant 打包报错:  [aapt] invalid resource directory nam

随机推荐

  1. Android(安卓)Jetpack组件之DataBinding
  2. android获取周围WIFI热点
  3. Android隐藏标题栏及解决启动闪过标题的
  4. TypeError:Cannot call method 'getItem'
  5. import android.support.v4.app.NavUtils
  6. 谈谈Android中的奇葩
  7. SDK is not loaded yet解决方法
  8. Android(安卓)Glide 使用
  9. Android(安卓)源码初步认识
  10. 高仿网易4.0新UI框架