当前位置:网站首页>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 :

TrimMemoryLevellevel Corresponding string constant level Corresponding number
TRIM_MEMORY_COMPLETECOMPLETE80
TRIM_MEMORY_MODERATEMODERATE60
TRIM_MEMORY_BACKGROUNDBACKGROUND40
TRIM_MEMORY_UI_HIDDENHIDDEN20
TRIM_MEMORY_RUNNING_CRITICALRUNNING_CRITICAL15
TRIM_MEMORY_RUNNING_LOWRUNNING_LOW10
TRIM_MEMORY_RUNNING_MODERATERUNNING_MODERATE5

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

原网站

版权声明
本文为[tinyvampirepudge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110747210987.html