Simulate low battery & low memory in Android

Android

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

To trigger your onTrimMemory callbacks:

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

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

Solution 2 - Android

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

Go to Settings > Developer options. Under the app section change the Background process limit to No background processes

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

Solution 3 - Android

yes, this api triggers the same callback you would get if you registered a context to ComponentCallback2, specifically the ComponentCallback2#onTrimMemory this 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+

Solution 4 - Android

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

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.

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

Solution 5 - Android

To simulate low Battery warning, try this command in the way answered by 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

Solution 6 - Android

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!

Solution 7 - Android

To trigger the memory trim event, an app can be used that fills all the RAM of the device, and that triggers the event.

There are many on the Play Store, they can be found by searching for 'fill ram'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser395494View Question on Stackoverflow
Solution 1 - AndroidShai BarackView Answer on Stackoverflow
Solution 2 - AndroidXavi GilView Answer on Stackoverflow
Solution 3 - AndroidyrizkView Answer on Stackoverflow
Solution 4 - AndroidDave WebbView Answer on Stackoverflow
Solution 5 - AndroidShahul3DView Answer on Stackoverflow
Solution 6 - AndroidFrankView Answer on Stackoverflow
Solution 7 - AndroidGeekaristView Answer on Stackoverflow