Android(安卓)格式化内部存储
Android 格式化内部存储
1,在设置界面点击>存储->格式化内部存储将会执行
packages/apps/Settings/src/com/android/settings/deviceinfo/Memory.java
- if(preference==mSdFormat){
- Intentintent=newIntent(Intent.ACTION_VIEW);
- intent.setClass(this,com.android.settings.MediaFormat.class);
- startActivity(intent);
- returntrue;
- }
2,packages/apps/Settings/src/com/android/settings/MediaFormat.java
下面将会跳到onCreate方法的establishInitialState();
- protectedvoidonCreate(BundlesavedState){
- @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); establishInitialState(); }
3,构造第一个含有一个按钮(格式化内部大容量存储设备)的页面,并为按钮添加事件
- privatevoidestablishInitialState(){
- 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,当点击这个按钮时将会跳到确认页面,防止误操作
- privateButton.OnClickListenermInitiateListener=newButton.OnClickListener(){
- publicvoidonClick(Viewv){
- if(!runKeyguardConfirmation(KEYGUARD_REQUEST)){
- establishFinalConfirmationState();
- }
- }
- };
5,确认格式化大容量存储设备的时候将会发送一个Intent,并将要格式化的路径传入
- Intentintent=newIntent(ExternalStorageFormatter.FORMAT_ONLY);
- intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
- StringstrPath=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");
- IMountServicemountService=getMountService();
- try{
- /*
- *Formountpointofexternalsdcardis:/mnt/sdcard/external_sd,
- *unmountsdcardfirst.
- */
- 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");
- finalIMountServicemountService=getMountService();
- finalStringextStoragePath=mPath;//Environment.getExternalStorageDirectory().toString();
- if(mountService!=null){
- newThread(){
- publicvoidrun(){
- booleansuccess=false;
- try{
- mountService.formatVolume(extStoragePath);
- success=true;
- }catch(Exceptione){
- 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(newIntent("android.intent.action.MASTER_CLEAR"));
- //Intenthandlingisasynchronous--assumeitwillhappensoon.
- stopSelf();
- return;
- }
- }
- //Ifwedidn'tsucceed,oraren'tdoingafullfactory
- //reset,thenitistimetoremountthestorage.
- if(!success&&mAlwaysReset){
- sendBroadcast(newIntent("android.intent.action.MASTER_CLEAR"));
- }
10,success等于真的话会会清楚缓存,语句mountService.formatVolume(extStoragePath);是格式化操作
- publicintformatVolume(StringmountPoint)throwsRemoteException{
- 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;
- }
更多相关文章
- 【Android】安卓中常用的图片加载方法
- 如何让Android横竖屏切换时不销毁当前activity
- Android(安卓)6.0 Ethernet流程分析记录
- android configChanges
- android 申请移动应用的签名生成方法
- Android(安卓)性能优化的一些方法
- Android(安卓)SDK下载和更新失败的解决方法
- AgentWeb WebView 与 Android交互 JS调用 Android
- android ant 打包报错: [aapt] invalid resource directory nam