最主要的方法

    /** * @Description 格式化 * @param path 路径 * @return true success ;false failure */    public boolean formatMedia(final String path) {        IMountService mountService =  getMountService();        int result = 0;        try {            //当卸载完sdcard后才能格式化sdcard - sdcard can be formatted only after the sdcard is unloaded            mountService.unmountVolume(path, true, false);            //给sdcard卸载的时间不能超过5s,超过5s后iMountService.formatVolume()不起作用。            Thread.sleep(4 * 1000);            result = mountService.formatVolume(path); //这步就是格式化            Log.d(TAG, "---------volume's path is " + path + "------format success ? " + result);            if (result == 0) { //如果成功卸载                Thread.sleep(4 * 1000);                int resultMount = mountService.mountVolume(path); //挂载回去                Log.d(TAG, "---------result_mount " + resultMount); //挂载结果            }        } catch (Exception ex) {            ex.printStackTrace();        }        if (result != 0) {            return true;        }        return false;    }    /** * @Description 得到IMountService * @return IMountService */    public synchronized IMountService getMountService() {        if (mMountService == null) {            IBinder iBinder = ServiceManager.getService("mount");            if (iBinder != null) {                 mMountService = IMountService.Stub.asInterface(iBinder);                if (mMountService == null) {                    Log.e(TAG, "Unable to connect to mount service! - is it running yet?");                }            }        }        return mMountService;     }

注意import

import android.os.IBinder;import android.os.storage.IMountService;import android.os.ServiceManager;

如果是要格式化所有设备,则可以用固有方法StorageVolume[] volumes = mountService.getVolumeList();获取储存信息,再用每一个volume.getPath();获得所有路径。

    /** * @Description format all the device */    public void formatMediaAll() {        IMountService mountService =  getMountService();        try {            StorageVolume[] volumes = mountService.getVolumeList();            for (StorageVolume volume : volumes) {                formatMedia(volume.getPath());            }        } catch (Exception e) {            e.printStackTrace();        }    }

注意import

import android.os.storage.StorageVolume;

上述代码验证有效,但是注意只能在源码中编译,直接在eclipse中写会报错,无法编译通过。

如何在源码编译

前提条件:服务器有源码可编译

CRT命令:    快速压缩:tar xvzf 压缩包名    快速删除:rm    快速重命名:mv 文件名.后缀 新文件名.后缀

首先要编译一下SDK(第一次需要,以后不需要)

1.进入源码目录

cd xxxxxxxxxxx/

2.整体编译SDK

source autocompile.sh

然后编译APP

1.从SecureCRT登陆服务器后,先切换到源码目录

cd xxxxxxxxxxx/

2.配置环境

source build/envsetup.sh
lunch XXXXXX-eng

注:以上步骤每次登陆编译机器,切换shell后,都需要执行上述操作配置环境变量

3.切换到项目文件夹,项目需包含配置好相关信息的Android.mk,并去掉bin包和gen包等无用信息,否则编译不通过

cd packages/apps/MyProjectName/

4.运行

mm

5.去输出文件夹找到apk

../out/target/product/XXXDevice/system/app/MyProject.apk

附件:完整代码VolumeManager.java

/****************************************************************** * *  @author: AZZ * *  @version: 1.0.0 * * Create at: 2015年6月8日 下午4:45:22 * * Revision: * * 2015年6月8日 下午4:45:22 * - first revision * *****************************************************************/import android.content.Context;import android.os.IBinder;import android.os.storage.IMountService;import android.os.ServiceManager;import android.os.storage.StorageManager;import android.os.storage.StorageVolume;import android.os.storage.StorageEventListener;import android.util.Log;/** * @ClassName VolumeManager * @Description 管理格式化的类 * @author AZZ * @Date 2015年6月8日 下午4:45:22 * @version 1.0.0 */public final class VolumeManager {    /** * @Field @TAG : TAG */    public static final String TAG = VolumeManager.class.getSimpleName();    /** * @Field @mMountService : mMountService */    private IMountService mMountService;    /** * @Field @unloadedDuration : unloaded device duration */    private final int unloadedDuration = 4 * 1000;    /** * @Field @mStorageManager : 存储管理者 */    private StorageManager mStorageManager;    /** * @Field @mStorageListener : 监听存储状态改变事件 */    private StorageEventListener mStorageListener = new StorageEventListener() {        @Override        public void onStorageStateChanged(final String path, final String oldState, final String newState) {            Log.i(TAG, "Received storage state changed notification that \""                    + path + "\" changed state from [" + oldState + "] to [" + newState + "]");        }    };    /** * @Description init * @param context context */    public VolumeManager(final Context context) {        mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);        mStorageManager.registerListener(mStorageListener);    }    /** * @Description 格式化 * @param path 路径 * @return true success ;false failure */    public boolean formatMedia(final String path) {        IMountService mountService =  getMountService();        int result = 0;        try {            //当卸载完sdcard后才能格式化sdcard - sdcard can be formatted only after the sdcard is unloaded            mountService.unmountVolume(path, true, false);            //给sdcard卸载的时间不能超过5s,超过5s后iMountService.formatVolume()不起作用。            Thread.sleep(unloadedDuration);            result = mountService.formatVolume(path);            Log.d(TAG, "---------volume's path is " + path + "------format success ? " + result);            if (result == 0) {                Thread.sleep(unloadedDuration);                int resultMount = mountService.mountVolume(path); //挂载回去                Log.d(TAG, "---------result_mount " + resultMount);            }        } catch (Exception ex) {            ex.printStackTrace();        }        if (result != 0) {            return true;        }        return false;    }    /** * @Description format all the device */    public void formatMediaAll() {        IMountService mountService =  getMountService();        try {            StorageVolume[] volumes = mountService.getVolumeList();            for (StorageVolume volume : volumes) {                boolean result = formatMedia(volume.getPath());            }        } catch (Exception e) {            e.printStackTrace();        }    }    /** * @Description 得到IMountService * @return IMountService */    public synchronized IMountService getMountService() {        if (mMountService == null) {            IBinder iBinder = ServiceManager.getService("mount");            if (iBinder != null) {                 mMountService = IMountService.Stub.asInterface(iBinder);                if (mMountService == null) {                    Log.e(TAG, "Unable to connect to mount service! - is it running yet?");                }            }        }        return mMountService;     }    /** * @Description 销毁方法 */    public void destroy() {        if (mStorageManager != null) {            mStorageManager.unregisterListener(mStorageListener);        }    }}

更多相关文章

  1. Android(安卓)SDK 源码下载,eclipse关联代码
  2. MTK Android(安卓)N 源码Rom Root
  3. [置顶] Android(安卓)IPC 通讯机制源码分析【下】
  4. 断网使用RycyclerView的jar包,运行时报错
  5. Android(安卓)Wifi --自动连接指定SSID(各种加密方式均可)
  6. 使用U3D 实现 Android(安卓)Launcher(提供源码)
  7. Android(安卓)Studio 使用平台特性的jar包
  8. Android(安卓)Zygote进程源码分析
  9. android 按行读取txt文本内容

随机推荐

  1. 一个Demo让你掌握所有的android控件
  2. Android高效率编码-findViewById()的蜕变
  3. Android 单独抽取 WebRtc-NS/NSX(音频降噪
  4. Android(安卓)WebView 通过post形式访问
  5. 调整Android音量等级及默认音量
  6. android系统部分广播
  7. Android实现微信分享及注意事项
  8. 【Android】数据存储之Files
  9. IBM网站文章: XML, JSON, ANDROID
  10. Android LinearLayout的布局属性介绍