編輯:關於Android編程
package cc.c;
import java.io.File;
import java.util.List;
import android.os.StatFs;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import android.os.Environment;
import android.content.Context;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
public class StorageUtil {
private static final int ERROR = -1;
public static int save_dir = 1;
//判斷是否已經安裝SD卡
public static boolean isSDCardExist() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
//內存清理
//注意權限:
//
public static void cleanMemory(Context context){
System.out.println("---> 清理前可用內存:"+getAvailMemory(context)+"MB");
ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
RunningAppProcessInfo runningAppProcessInfo=null;
List runningAppProcessInfoList= activityManager.getRunningAppProcesses();
//List serviceInfos = mActivityManager.getRunningServices(100);
if (runningAppProcessInfoList != null) {
for (int i = 0; i < runningAppProcessInfoList.size(); ++i) {
runningAppProcessInfo= runningAppProcessInfoList.get(i);
// 一般數值大於RunningAppProcessInfo.IMPORTANCE_SERVICE
// 的進程即為長時間未使用進程或者空進程
// 一般數值大於RunningAppProcessInfo.IMPORTANCE_VISIBLE
// 的進程都是非可見進程,即在後台運行
if (runningAppProcessInfo.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
String[] pkgList = runningAppProcessInfo.pkgList;
for (int j = 0; j < pkgList.length; ++j) {
System.out.println("===> 正在清理:"+pkgList[j]);
activityManager.killBackgroundProcesses(pkgList[j]);
}
}
}
}
System.out.println("---> 清理後可用內存:"+getAvailMemory(context)+"MB");
}
//獲取內存總大小
public static long getTotalMemory() {
//系統的內存信息文件
String filePath = "/proc/meminfo";
String lineString;
String[] stringArray;
long totalMemory = 0;
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader,1024 * 8);
//讀取meminfo第一行,獲取系統總內存大小
lineString = bufferedReader.readLine();
//按照空格拆分
stringArray = lineString.split("\\s+");
//獲得系統總內存,單位KB
totalMemory = Integer.valueOf(stringArray[1]).intValue();
bufferedReader.close();
} catch (IOException e) {
}
return totalMemory / 1024;
}
//獲取可用內存大小
public static long getAvailMemory(Context context) {
ActivityManager activityManager=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo.availMem / (1024 * 1024);
}
//SD卡剩余空間
public static long getAvailableExternalMemorySize() {
if (isSDCardExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
} else {
return ERROR;
}
}
//SD卡總空間
public static long getTotalExternalMemorySize() {
if (isSDCardExist()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return totalBlocks * blockSize;
} else {
return ERROR;
}
}
//判斷SD卡下external_sd文件夾的總大小
public static long getTotalExternal_SDMemorySize() {
if (isSDCardExist()) {
File path = Environment.getExternalStorageDirectory();
File externalSD = new File(path.getPath() + "/external_sd");
if (externalSD.exists() && externalSD.isDirectory()) {
StatFs stat = new StatFs(path.getPath() + "/external_sd");
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
if (getTotalExternalMemorySize() != -1
&& getTotalExternalMemorySize() != totalBlocks
* blockSize) {
return totalBlocks * blockSize;
} else {
return ERROR;
}
} else {
return ERROR;
}
} else {
return ERROR;
}
}
//判斷SD卡下external_sd文件夾的可用大小
public static long getAvailableExternal_SDMemorySize() {
if (isSDCardExist()) {
File path = Environment.getExternalStorageDirectory();
File externalSD = new File(path.getPath() + "/external_sd");
if (externalSD.exists() && externalSD.isDirectory()) {
StatFs stat = new StatFs(path.getPath() + "/external_sd");
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
if (getAvailableExternalMemorySize() != -1
&& getAvailableExternalMemorySize() != availableBlocks
* blockSize) {
return availableBlocks * blockSize;
} else {
return ERROR;
}
} else {
return ERROR;
}
} else {
return ERROR;
}
}
}
Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法
Android中的不同Activity之間傳遞對象,我們可以考慮采用Bundle.putSerializable(Key,Object);也可以考慮采用Bun
Android-廣播機制的注冊及作用
Android的廣播接收器注冊方式分為兩種: 1.動態注冊:(即代碼注冊,該注冊經常伴隨著組件的生命周期或者對象的生命周期同生共死),如下: /** * @autho
小米電視2s和小米電視2區別
小米電視2s定價在2999很大程度上是小米電視2s功能的刪減,其中大家最為關注的是砍掉了3D功能,3d功能可能不是每個人都需要,但是有總比沒有要好嗎?你說對
Android高效率編碼-第三方SDK詳解系列(三)——JPush推送牽扯出來的江湖恩怨,XMPP實現推送,自定義客戶端推送
很久沒有更新第三方SDK這個系列了,所以更新一下這幾天工作中使用到的推送,寫這個系列真的很要命,你要去把他們的API文檔大致的翻閱一遍,而且各種功能都實現一遍,解決各種b