最主要的方法
/**
* @Description 格式化
* @param path 路徑
* @return true success ;false failure
*/
public boolean formatMedia(final String path) {
IMountService mountService = getMountService();
int result = ;
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( * );
result = mountService.formatVolume(path); //這步就是格式化
Log.d(TAG, "---------volume's path is " + path + "------format success ? " + result);
if (result == ) { //如果成功解除安裝
Thread.sleep( * );
int resultMount = mountService.mountVolume(path); //挂載回去
Log.d(TAG, "---------result_mount " + resultMount); //挂載結果
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (result != ) {
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 = * ;
/**
* @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 = ;
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 == ) {
Thread.sleep(unloadedDuration);
int resultMount = mountService.mountVolume(path); //挂載回去
Log.d(TAG, "---------result_mount " + resultMount);
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (result != ) {
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);
}
}
}