[Android][客需]内存释放
客户无感知的杀后台机制,当RAM使用率超过60%,自动关闭5分钟未使用的APP
直接上源码 懒得解析 有空再写
//SW wenjie.gu add for sth start
private void RamListener(){
new Thread() {
public void run(){
while(ProjectConfig.DEFAULT_SUPPORT_RAMCLEAN){
try {
getTotalRam();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
private void getTotalRam() throws Exception {
IsKillingPg=true;
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
manager.getMemoryInfo(info);
long afterMem = getAvailMemory(mContext);
double a= info.availMem/1024/1024;
double b= info.totalMem/1024/1024;
double Tatal = MyMath.percentage(a, b, 2);
if (Tatal < 40) {
Log.e("wenjie.gu", "getTotalRam ClearUP!");
ClearUP();
Log.w("wenjie.gu", "清理后可用内存百分比为---->: " + Tatal);
Thread.sleep (180000);
}
IsKillingPg=false;
}
public static class MyMath {
private MyMath() {
}
public static double percentage(double d1, double zs, int dot) {
double bs = Math.pow(10.0, toDouble(dot + 2, 0.0));
double cs = Math.pow(10.0, toDouble(dot, 0.0));
double num = 0.0;
if (zs > 0) {
num = Math.floor(d1 * bs / zs) / cs;
}
return num;
}
public static double percentage(int d1, int zs, int dot) {
double bs = Math.pow(10.0, toDouble(dot + 2, 0.0));
double cs = Math.pow(10.0, toDouble(dot, 0.0));
double num = 0.0;
if (zs > 0) {
num = Math.floor(d1 * bs / zs) / cs;
}
return num;
}
private static Double toDouble(Object value, Double defaultValue) {
if (null == value) {
return defaultValue;
}
return Double.parseDouble(value.toString());
}
}
private void ClearUP()throws ParseException{
long oneday =86400000;
long fivemin =300000;
UsageStatsManager usageManager=(UsageStatsManager) mContext.getSystemService(Context.USAGE_STATS_SERVICE);
if (usageManager != null && IsKillingPg) {
int intervalType = UsageStatsManager.INTERVAL_BEST;
List<UsageStats> applicationList = usageManager.queryUsageStats(intervalType,System.currentTimeMillis()-86400000, System.currentTimeMillis());
Log.w("wenjie.gu","现在的时间 ---> "+longToDate(System.currentTimeMillis()));
Log.w("wenjie.gu","现在的时间 ---> "+System.currentTimeMillis());
for (UsageStats usageStats : applicationList) {
long UnusedTime=(System.currentTimeMillis()-usageStats.getLastTimeUsed());
if(UnusedTime>fivemin && UnusedTime<oneday){
String unusePkname=usageStats.getPackageName();
mList.add(unusePkname);
Log.w("wenjie.gu", "最后使用时间为---->"+unusePkname+"时间为--->"+longToDate(usageStats.getLastTimeUsed()));
Log.w("wenjie.gu", "最后使用时间为---->"+unusePkname+"时间为--->"+usageStats.getLastTimeUsed());
}
}
}
ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
long beforeMem = getAvailMemory(mContext);
Log.i("wenjie.gu", "清理前可用内存为 ---->: " + beforeMem);
for (ActivityManager.RunningAppProcessInfo appProcessInfo:appProcessInfos) {
if(appProcessInfo.processName.contains("com.android.system") ||appProcessInfo.pid==android.os.Process.myPid())
continue;
String[] pkNameList=appProcessInfo.pkgList;
for(int i=0;i<pkNameList.length;i++){
String pkName=pkNameList[i];
Method method = null;
if(!mList.contains(pkName)||isRunningForeground(pkName)){
Log.e("wenjie.gu","进程--->"+pkName+"被排除!不被清理!");
continue;
}
if(mList.contains(pkName)&&(!pkName.equals("com.huace.landstar"))&&(!pkName.contains("com.android.system"))){
try {
method = Class.forName("android.app.ActivityManager").getMethod("forceStopPackage", String.class);
method.invoke(activityManager, pkName);
Log.e("wenjie.gu","进程--->"+pkName+"被清理!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Log.i("wenjie.gu", "清理后可用内存为 ---->: " + beforeMem);
}
private long getAvailMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.availMem;
}
private String formatFileSize(long number){
return Formatter.formatFileSize(mContext, number);
}
public static Date longToDate(long lo) throws ParseException {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//long转Date
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sd.format(new Date(lo)));
return date;
}
private boolean isRunningForeground(String packkname) {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if (currentPackageName != null && currentPackageName.equals(packkname)){
return true;
}
return false;
}
//SW wenjie.gu add for sth end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!