这个需求实现起来也挺容易的,实现逻辑就是自己写一个弹窗,然后找到一个实时检测内存的地方,去弹出一次内存不足的弹窗就可以了,下面直接展示解决方法
diff --git a/frameworks/base/core/res/res/layout/dialog_80.xml b/frameworks/base/core/res/res/layout/dialog_80.xml
new file mode 100755
index 00000000000..ccaf153c382
--- /dev/null
+++ b/frameworks/base/core/res/res/layout/dialog_80.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:padding="10dp"
+ android:orientation="vertical">
+
+ <TextView
+ android:id="@+id/tv_title"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_horizontal"
+ android:textStyle="bold"
+ android:textSize="18dp">
+ </TextView>
+
+ <TextView
+ android:id="@+id/tv_content"
+ android:layout_marginTop="10dp"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:gravity="center_horizontal"
+ android:textSize="16dp">
+ </TextView>
+
+</LinearLayout>
\ No newline at end of file
diff --git a/frameworks/base/core/res/res/values/symbols.xml b/frameworks/base/core/res/res/values/symbols.xml
old mode 100644
new mode 100755
index ff6373b7d62..9f4a74a34a0
--- a/frameworks/base/core/res/res/values/symbols.xml
+++ b/frameworks/base/core/res/res/values/symbols.xml
@@ -251,6 +251,8 @@
<java-symbol type="id" name="profile_tabhost" />
<java-symbol type="id" name="tabs" />
<java-symbol type="id" name="tabcontent" />
+ <java-symbol type="id" name="tv_title" />
+ <java-symbol type="id" name="tv_content" />
<java-symbol type="attr" name="actionModeShareDrawable" />
<java-symbol type="attr" name="alertDialogCenterButtons" />
@@ -1548,6 +1550,7 @@
<java-symbol type="layout" name="restrictions_pin_setup" />
<java-symbol type="layout" name="immersive_mode_cling" />
<java-symbol type="layout" name="user_switching_dialog" />
+ <java-symbol type="layout" name="dialog_80" />
<java-symbol type="layout" name="common_tab_settings" />
<java-symbol type="layout" name="notification_material_media_seekbar" />
<java-symbol type="layout" name="resolver_list_per_profile" />
diff --git a/frameworks/base/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java b/frameworks/base/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java
old mode 100644
new mode 100755
index 13e5a61a090..eda3943ca1e
--- a/frameworks/base/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java
+++ b/frameworks/base/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java
@@ -61,6 +61,22 @@ import java.io.PrintWriter;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
+//swl add
+import android.os.Looper;
+import com.android.internal.R;
+import android.content.IntentFilter;
+import android.os.Build;
+import android.os.Bundle;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.view.WindowManager;
+import android.view.View.OnClickListener;
+import android.view.LayoutInflater;
+import android.widget.TextView;
+import android.view.View;
+import android.widget.Button;
+import android.util.Log;
+//end
/**
* Service that monitors and maintains free space on storage volumes.
@@ -71,6 +87,7 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
public class DeviceStorageMonitorService extends SystemService {
private static final String TAG = "DeviceStorageMonitorService";
+ private static AlertDialog alertDialog;
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
@@ -79,6 +96,7 @@ public class DeviceStorageMonitorService extends SystemService {
public static final String EXTRA_SEQUENCE = "seq";
private static final int MSG_CHECK = 1;
+ boolean FLAG_SHOWDIALOG_ONCE = true;
private static final long DEFAULT_LOG_DELTA_BYTES = DataUnit.MEBIBYTES.toBytes(64);
private static final long DEFAULT_CHECK_INTERVAL = DateUtils.MINUTE_IN_MILLIS;
@@ -211,6 +229,16 @@ public class DeviceStorageMonitorService extends SystemService {
// of any relevant broadcasts.
oldLevel = State.LEVEL_UNKNOWN;
newLevel = mForceLevel;
+ //swl add
+ } else if (usableBytes <= totalBytes*0.247 && FLAG_SHOWDIALOG_ONCE ){
+ Log.d("swl","start");
+ Log.d("swl","usableBytes + " + usableBytes);
+ Log.d("swl","totalBytes + " + totalBytes);
+ showDialog();
+ Log.d("swl","end");
+ FLAG_SHOWDIALOG_ONCE = false;
+ newLevel = State.LEVEL_FULL;
+ //end
} else if (usableBytes <= fullBytes) {
newLevel = State.LEVEL_FULL;
} else if (usableBytes <= lowBytes) {
@@ -258,6 +286,7 @@ public class DeviceStorageMonitorService extends SystemService {
switch (msg.what) {
case MSG_CHECK:
check();
+ Log.d("swl","DSMS");
return;
}
}
@@ -565,6 +594,45 @@ public class DeviceStorageMonitorService extends SystemService {
getContext().sendBroadcastAsUser(notFullIntent, UserHandle.ALL);
}
}
+ //swl add
+ public void showDialog(){
+ if(alertDialog != null && alertDialog.isShowing())
+ return;
+ AlertDialog.Builder b = new AlertDialog.Builder(getContext());
+ b.setCancelable(true);
+ View view = View.inflate(getContext(), com.android.internal.R.layout.dialog_80,null);
+ TextView titleTv = (TextView)view.findViewById(com.android.internal.R.id.tv_title);
+ titleTv.setText("Important Tip!");
+ TextView contentTv = (TextView)view.findViewById(com.android.internal.R.id.tv_content);
+ contentTv.setText("The device's memory has reached 80% of its capacity, free up space to guarantee the good performance of your product.");
+ b.setView(view);
+ final StorageManager storage0 = getContext().getSystemService(StorageManager.class);
+ Log.d("swl","showDialog");
+ //开启新的线程,防止弹出dialog时所依附的view未完成创建
+ new Thread(new Runnable(){
+ @Override
+ public void run() {
+ Looper.prepare();
+ try {
+ Thread.sleep(10000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ Log.d("swl", "1");
+ AlertDialog d = b.create();
+ Log.d("swl", "2");
+ d.setCanceledOnTouchOutside(true);
+ d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//全局对话框
+ Log.d("swl", "3");
+ d.show();
+ alertDialog = d;
+ Log.d("swl", "内存不足");
+ Looper.loop();
+ }
+ }).start();
+}
+ //end
private static class CacheFileDeletedObserver extends FileObserver {
public CacheFileDeletedObserver() {
有大佬知道其他更好的方法,可以dddd(带带弟弟)