在Android中模拟低电量和低内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3656594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:12:28  来源:igfitidea点击:

Simulate low battery & low memory in Android

android

提问by user395494

In order to generate the notifications i need to know about how to generate the low battery and low memory interrupts programmatically. Can any one please provide your suggestions.I am aware of Intents.

为了生成通知,我需要了解如何以编程方式生成低电量和低内存中断。任何人都可以提供您的建议。我知道意图。

回答by Shai Barack

To trigger your onTrimMemorycallbacks:

要触发您的onTrimMemory回调:

adb shell am send-trim-memory <process-name> <level>

adb shell am send-trim-memory <process-name> <level>

e.g. adb shell am send-trim-memory com.example.app MODERATE

例如 adb shell am send-trim-memory com.example.app MODERATE

回答by Xavi Gil

Low memory can also be simulated using Background process limitunder the device developer options.

也可以Background process limit在设备开发者选项下使用模拟低内存。

Go to Settings > Developer options. Under the appsection change the Background process limitto No background processes

Settings > Developer options。在该app部分下将更Background process limit改为无后台进程

Now your activity will be killed every time you switch to another app. Useful for testing state saving and state restoration.

现在,每次切换到另一个应用程序时,您的活动都会被终止。用于测试状态保存和状态恢复。

回答by yrizk

yes, this api triggers the same callback you would get if you registered a context to ComponentCallback2, specifically the ComponentCallback2#onTrimMemorythis wasn't mentioned here, so I thought I'd make it clear. The syntax for this command is: am send-trim-memory [--user <USER_ID>] <PROCESS> [HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE] Note: this command is only available on devices running Marshmallow+

是的,如果您将上下文注册到 ComponentCallback2,这个 api 会触发相同的回调,特别是ComponentCallback2#onTrimMemory这在这里没有提到,所以我想我会说清楚。此命令的语法是: am send-trim-memory [--user <USER_ID>] <PROCESS> [HIDDEN|RUNNING_MODERATE|BACKGROUND|RUNNING_LOW|MODERATE|RUNNING_CRITICAL|COMPLETE] 注意:此命令仅在运行 Marshmallow+ 的设备上可用

回答by Dave Webb

On the Android Emulator you can set the power status by connecting to the Emulator console and using the powercommand.

在 Android Emulator 上,您可以通过连接到 Emulator 控制台并使用命令设置电源状态power

As far as low memory goes, you just need to make sure that your application can handle being killed without warning when it is in the background. Testing this is one of the very few cases that actually call for a Task Manager on Android, or if you're running Android 2.2 you can kill applications via Settings.

就低内存而言,您只需要确保您的应用程序可以在后台处理被杀死而没有警告。测试这是在 Android 上实际需要任务管理器的极少数情况之一,或者如果您运行的是 Android 2.2,您可以通过设置终止应用程序。

There are ways of reducing the memory available to applicationsbut I think they're unnecessary.

有一些方法可以减少应用程序可用的内存,但我认为它们是不必要的。

回答by Shahul3D

To simulate low Battery warning, try this command in the way answered by Frank:

要模拟低电量警告,请按照 Frank 回答的方式尝试此命令:

power capacity 10 // It will set the battery level into 10%

For low Memory:

对于低内存:

ulimit -Sv 15000  //The current memory limit will set to 15000 Kb

回答by Frank

You can use the emulator menu is shown on my blog posting. Just telnet to localhost on the port of your emulator (default is 5554) and then type help. Follow the instructions from there!

您可以使用我的博客文章中显示的模拟器菜单。只需 telnet 到模拟器端口上的 localhost(默认为 5554),然后输入 help。按照那里的说明进行操作!