当前位置:网站首页>Receive ontrimmemory and other callbacks through componentcallbacks2 and mock the corresponding scenario
Receive ontrimmemory and other callbacks through componentcallbacks2 and mock the corresponding scenario
2022-06-11 07:55:00 【tinyvampirepudge】
We're doing it app When there is not enough memory , Need to do some memory release operations , To avoid app Carton , Or delay as much as possible app Survival time , Reduce the probability of being recycled by the system .
How to listen ComponentCallbacks
So how to monitor these opportunities ? Systematic Application、Activity、Service and ContentProvider All of them are implemented ComponentCallbacks2 Interface , We can easily access these opportunities . In addition to these opportunities , We can also go through Context#registerComponentCallbacks To add your own monitor .
In general, we add ComponentCallbacks2 that will do ,ComponentCallbacks2 It's not just onTrimMemory(@TrimMemoryLevel int level) Method ,, It also inherits ComponentCallbacks Interface , So you can also listen to the system onConfigurationChanged(@NonNull Configuration newConfig) and onLowMemory() Callback .
In these callbacks , We can do some operations to release memory and resources .
ComponentCallbacks2 Source code :
public interface ComponentCallbacks2 extends ComponentCallbacks {
/** @hide */
@IntDef(prefix = { "TRIM_MEMORY_" }, value = {
TRIM_MEMORY_COMPLETE,
TRIM_MEMORY_MODERATE,
TRIM_MEMORY_BACKGROUND,
TRIM_MEMORY_UI_HIDDEN,
TRIM_MEMORY_RUNNING_CRITICAL,
TRIM_MEMORY_RUNNING_LOW,
TRIM_MEMORY_RUNNING_MODERATE,
})
@Retention(RetentionPolicy.SOURCE)
public @interface TrimMemoryLevel {}
static final int TRIM_MEMORY_COMPLETE = 80;
static final int TRIM_MEMORY_MODERATE = 60;
static final int TRIM_MEMORY_BACKGROUND = 40;
static final int TRIM_MEMORY_UI_HIDDEN = 20;
static final int TRIM_MEMORY_RUNNING_CRITICAL = 15;
static final int TRIM_MEMORY_RUNNING_LOW = 10;
static final int TRIM_MEMORY_RUNNING_MODERATE = 5;
void onTrimMemory(@TrimMemoryLevel int level);
}
ComponentCallbacks Source code :
public interface ComponentCallbacks {
void onConfigurationChanged(@NonNull Configuration newConfig);
void onLowMemory();
}
how mock What about the timing ?
The operations of killing processes on the Internet are the same , And it can't be mock All the time , So it's not accepted here .
After unremitting search , I found one that could mockonTrimMemory(@TrimMemoryLevel int level) All of the methods level The order of , The specific format is as follows :
adb shell am send-trim-memory <package-name> <level>
e.g.: level It can be Constant string It can also be the corresponding Numbers , Follow ComponentCallbacks2.TrimMemoryLevel The values in are one-to-one
adb shell am send-trim-memory com.tinytongtong.androidstudy MODERATE
perhaps :
adb shell am send-trim-memory com.tinytongtong.androidstudy 5
level The specific mapping relationship is shown in the table :
| TrimMemoryLevel | level Corresponding string constant | level Corresponding number |
|---|---|---|
| TRIM_MEMORY_COMPLETE | COMPLETE | 80 |
| TRIM_MEMORY_MODERATE | MODERATE | 60 |
| TRIM_MEMORY_BACKGROUND | BACKGROUND | 40 |
| TRIM_MEMORY_UI_HIDDEN | HIDDEN | 20 |
| TRIM_MEMORY_RUNNING_CRITICAL | RUNNING_CRITICAL | 15 |
| TRIM_MEMORY_RUNNING_LOW | RUNNING_LOW | 10 |
| TRIM_MEMORY_RUNNING_MODERATE | RUNNING_MODERATE | 5 |
Examples of all commands :
adb shell am send-trim-memory com.tinytongtong.androidstudy COMPLETE
adb shell am send-trim-memory com.tinytongtong.androidstudy MODERATE
adb shell am send-trim-memory com.tinytongtong.androidstudy BACKGROUND
adb shell am send-trim-memory com.tinytongtong.androidstudy HIDDEN
adb shell am send-trim-memory com.tinytongtong.androidstudy RUNNING_CRITICAL
adb shell am send-trim-memory com.tinytongtong.androidstudy RUNNING_LOW
adb shell am send-trim-memory com.tinytongtong.androidstudy RUNNING_MODERATE
Reference resources
https://stackoverflow.com/questions/3656594/simulate-low-battery-low-memory-in-android
边栏推荐
- Tidb cloud launched Google cloud marketplace, empowering global developers with a new stack of real-time HTAP databases
- JSP technology: JSP overview, JSP basic syntax, JSP instructions, JSP implicit objects, JSP action elements
- Request request object and response response object
- Getting started with bladed tutorial (video)
- [cluster] haproxy load balancing
- Post - payload of interface test
- Modular linear equations (Chinese remainder theorem + general solution)
- TypeScript-分布式条件类型
- TypeScript-头文件使用细节
- Image processing operation record
猜你喜欢

Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本

Tutoriel de démarrage bladed (vidéo)

Alchemy experience (model training of deep learning) the necessity of timely adjusting training parameters for some situations (the adjustment of learning rate LR is the primary) summarizes some metho

(transformation) tree, binary tree and forest transformation principle

How to output the percent sign "%" in printf function in C language

Using Tkinter to realize guessing numbers game

Paging of the flask page
![[atcoder2306] rearranging (topology)](/img/b3/38589a07a7c26bea8ed154ab794760.png)
[atcoder2306] rearranging (topology)

Figure seamless database integration tushare interface

图数据库无缝集成Tushare接口
随机推荐
[atcoder2305] declining (game)
TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者
Return in foreach and break in for
批量拼接字符串
TypeScript-声明合并
TypeScript-接口和类型别名异同
After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
Collation of basic knowledge of intermediate development of Andrews (for interview)
Niuke JS wrong question knowledge point record
Sort - merge sort
multi-sig SC
JSP development model
Xshell7 and xftp7 to continue using this program, you must apply the latest updates or use a new version
2022.6.7 special student simulation
How to prepare for the new PMP syllabus exam?
E regression test of this featured app
El expressions and JSTL
Switch statement
TypeScript-null和undefined
如何做好空状态设计?来看这份全面总结