1. android 获取指定磁盘内存的工具类【兼容API18以上】
Const.ANDROID_SDK_INT = 18 api18
package com.apktv.market.util;
import com.apktv.market.consts.Const;
import android.os.Build;
import android.os.StatFs;
public class MemoryInfoUtils {
private static MemoryInfoUtils instance = new MemoryInfoUtils();
private MemoryInfoUtils(){
}
public static MemoryInfoUtils getInstance(){
return instance;
}
/**
* @param path 磁盘的路径
* @return 该磁盘的总容量
*/
public long getTotalSize(String path){
StatFs sf = new StatFs(path);
long totalBlockCount;
long blockSize;
if (Build.VERSION.SDK_INT >= Const.ANDROID_SDK_INT) {
totalBlockCount = sf.getBlockCountLong();
blockSize = sf.getBlockSizeLong();
}
else{
totalBlockCount = sf.getBlockCount();
blockSize = sf.getBlockSize();
}
return (totalBlockCount*blockSize)/1024/1024;
}
/**
* @param path 磁盘路径
* @return 该磁盘剩余可用的容量
*/
public long getAvaiableSize(String path){
StatFs sf = new StatFs(path);
long avaiLableBlockCount;
long blockSize;
if (Build.VERSION.SDK_INT >= Const.ANDROID_SDK_INT) {
avaiLableBlockCount = sf.getAvailableBlocksLong();
blockSize = sf.getBlockSizeLong();
}
else{
avaiLableBlockCount = sf.getAvailableBlocks();
blockSize = sf.getBlockSize();
}
return (avaiLableBlockCount*blockSize)/1024/1024;
}
}
2.SharedPreference工具类
package com.hp.lessonhelper.utils;
import com.hp.draftpaper.App.App;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* @author tangdekun
* SharedPreferences工具类
*
*/
public class SharedPreferencesUtils {
private static SharedPreferencesUtils instance;
private Context context;
private static final String FILE_NAME = "share_data";
private SharedPreferences mPreferences;
private Editor mEditor;
private SharedPreferencesUtils() {
context = App.AppContext;
mPreferences = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
mEditor = mPreferences.edit();
}
public static SharedPreferencesUtils getInstance() {
if (instance == null) {
instance = new SharedPreferencesUtils();
}
return instance;
}
public void put(String key, Object object) {
String type = object.getClass().getSimpleName();
if ("String".equals(type)) {
mEditor.putString(key, (String) object);
} else if ("Integer".equals(type)) {
mEditor.putInt(key, (Integer) object);
} else if ("Boolean".equals(type)) {
mEditor.putBoolean(key, (Boolean) object);
} else if ("Float".equals(type)) {
mEditor.putFloat(key, (Float) object);
} else if ("Long".equals(type)) {
mEditor.putLong(key, (Long) object);
}
mEditor.commit();
}
public Object get(String key, Object defaultObject) {
String type = defaultObject.getClass().getSimpleName();
if ("String".equals(type)) {
return mPreferences.getString(key, (String) defaultObject);
} else if ("Integer".equals(type)) {
return mPreferences.getInt(key, (Integer) defaultObject);
} else if ("Boolean".equals(type)) {
return mPreferences.getBoolean(key, (Boolean) defaultObject);
} else if ("Float".equals(type)) {
return mPreferences.getFloat(key, (Float) defaultObject);
} else if ("Long".equals(type)) {
return mPreferences.getLong(key, (Long) defaultObject);
}
return null;
}
}