user395494
user395494

Reputation: 681

Simulate low battery & low memory in 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.

Upvotes: 68

Views: 55976

Answers (9)

twiceYuan
twiceYuan

Reputation: 883

I created an App to simulate the low memory situation of being killed by the system lmdk process, you can download it from here: https://play.google.com/store/apps/details?id=io.github.twiceyuan.ramkiller

Just click on the app's unique button and wait for your app to be killed (you can see this event in Logcat with a filtered tag of "lmdk").

Upvotes: 0

Shai Barack
Shai Barack

Reputation: 707

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 RUNNING_MODERATE

Upvotes: 66

Stan
Stan

Reputation: 2181

A great way to make sure the app is actually killed is to... kill it.

As of now (Dec 2023), you can use the "kill process" context menu option inside the Logcat window.

  1. Hide your app
  2. Kill it, using this menu
  3. Resume your app
  4. -> Application.onCreate() is called
  5. -> Your app will resume to its original state (if it handles state well)

Use "Kill process", as "Force stop application" will not allow you to resume to the previous state.

P.S. Not sure why, but I don't see this option elsewhere in the GUI. enter image description here

Upvotes: 0

Frank
Frank

Reputation: 1426

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

Upvotes: 1

Geekarist
Geekarist

Reputation: 849

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'.

Upvotes: 1

yrizk
yrizk

Reputation: 394

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+

Upvotes: 16

Xavi Gil
Xavi Gil

Reputation: 11568

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.

Upvotes: 55

Shahul3D
Shahul3D

Reputation: 2149

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

Upvotes: 4

David Webb
David Webb

Reputation: 193714

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.

Upvotes: 6

Related Questions