AbsActivity新增低内存回调

DeviceUtils新增获取当前应用是否处于低内存判断方法
This commit is contained in:
zlzw 2022-10-31 14:59:14 +08:00
parent e70a5e09f6
commit 7e9b794c7f
2 changed files with 25 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
@ -54,8 +55,8 @@ public abstract class AbsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Intent intent=getIntent();
if(intent!=null) {
Intent intent = getIntent();
if (intent != null) {
isFullWindow = getIntent().getBooleanExtra("isFull", false);
if (isFullWindow) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
@ -358,4 +359,15 @@ public abstract class AbsActivity extends AppCompatActivity {
return result;
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
Log.i("memory", "onTrimMemory: " + level);
switch (level) {
case TRIM_MEMORY_RUNNING_CRITICAL://内存低值危险值
break;
case TRIM_MEMORY_RUNNING_LOW://内存过低
break;
}
}
}

View File

@ -1,6 +1,7 @@
package com.yunbao.common.utils;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@ -69,4 +70,14 @@ public class DeviceUtils {
return "0";
}
/**
* 当前是否处于低内存状态
*/
public static boolean isLowMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return mi.lowMemory;// 将获取的内存大小规格化
}
}