Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate low battery & low memory in Android

Tags:

android

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.

like image 569
user395494 Avatar asked Sep 07 '10 07:09

user395494


People also ask

How do you simulate low battery?

With the Android Emulator's Extended Controls, it's now possible to set the battery level with a GUI slider called "Charge Level." To access this, start the emulator. Then click the "..." at the bottom of the settings panel (which hovers to the right of the emulator). The Charge Level slider goes from 0 to 100%.

How do you simulate a battery?

The simplest way to simulate a battery is with a resistor. This works fine in cases where the charger is a series resistor. The resistor that simulates the battery sinks the charge current, and you can measure the voltage drop across the resistor to calculate the charge current.


6 Answers

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

like image 190
Shai Barack Avatar answered Oct 07 '22 06:10

Shai Barack


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.

like image 20
Xavi Gil Avatar answered Oct 07 '22 06:10

Xavi Gil


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+

like image 33
yrizk Avatar answered Oct 07 '22 05:10

yrizk


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.

like image 23
Dave Webb Avatar answered Oct 07 '22 07:10

Dave Webb


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
like image 35
Shahul3D Avatar answered Oct 07 '22 07:10

Shahul3D


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!

like image 37
Frank Avatar answered Oct 07 '22 06:10

Frank