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.
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
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.
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+
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.
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
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!
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'.
Related
When forcing the app standby-bucket
adb shell am set-standby-bucket <package> rare
and then getting the stand by mode using
adb shell am get-standby-bucket <package>
adb returns always 5 regardless of the set mode.
What does 5 mean and how to get this work as it should be? 5 is none of specified modes: https://developer.android.com/reference/android/app/usage/UsageStatsManager.html#getAppStandbyBucket()
Sometimes this works as it should be for a very short period of time after removing and reinstalling the app but then stops working for unknown reason.
The information is not depicted as clearly as it probably could be, but in the link you provided, under STANDBY_BUCKET_ACTIVE, it says:
Standby bucket values that are ≤ STANDBY_BUCKET_ACTIVE will not be throttled by the system while they are in this bucket. Buckets > STANDBY_BUCKET_ACTIVE will most likely be restricted in some way. For instance, jobs and alarms may be deferred.
That is, 5 means no throttling.
Google explains how to control many parameters of an Android emulator, like the current charge of the battery, the sensor input and so on here: https://developer.android.com/studio/run/emulator-console.html .
I would like to create an Espresso test that changes these parameters of an emulator during a test, is this possible? I know that there is the following method:
InstrumentationRegistry.getInstrumentation().uiAutomation
.executeShellCommand("someShellCommand")
This method can be called during a test, but for changing system parameters of an emulator I first have to login via telnet localhost 5554 and only then can I manipulate the emulator parameters like, for example, so: power capacity 30, which changes the power of the battery to 30%. How can I do this during an Espresso test?
You can try
InstrumentationRegistry.getInstrumentation().getUiAutomation()
.executeShellCommand("dumpsys battery set level 30");
to change the battery level. Other values or settings may require different commands.
Note: It is your responsibility to close the returned file descriptor once you are done reading.
I am trying to find if I can enable and/or disable Android's built-in "Battery Saver" mode programmatically.
Any official approaches, or trickery, are welcome.
For reference, here is how to do it following Android's standard UI in 5.0:
http://www.androidcentral.com/android-50-lollipop-basics-how-get-more-life-between-charges-battery-saver
I am aware you can detect it -- that is not what I am after.
Thanks all.
You can enable/disable Battery Saver programmatically on rooted devices. You have to edit the low_power value in global table, in /data/data/com.android.providers.settings/databases/settings.db file.
If your device supports settings command, you can execute (as root):
settings put global low_power 1
to enable Energy Saver and
settings put global low_power 0
to disable it.
If it doesn't, use sqlite3 command:
sqlite3 /data/data/com.android.providers.settings/databases/settings.db "update global set value='1' where name='low_power';"
sqlite3 /data/data/com.android.providers.settings/databases/settings.db "update global set value='0' where name='low_power';"
Remember that you have to unplug your phone from PC first, otherwise system will disable Energy Saver. Use ADB over WiFi or Android Terminal (Emulator).
UPDATE:
The sqlite3 method doesn't seem to be reliable anymore.
I'm not sure if android.os.action.POWER_SAVE_MODE_CHANGED broadcast gets send. Maybe you have to send it manually, like in code from here:
private static String COMMAND_ENABLE = "settings put global low_power 1\n" +
"am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode true\n";
private static String COMMAND_DISABLE = "settings put global low_power 0\n" +
"am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode false\n";
Also, it's been reported that a new power_saving entry appeared in settings database, however in Android 6.0.1 I haven't managed to find it. In Android 7.1.1_r13 low_power is still used internally (e.g. here), however it may get changed in some Android release. You may want to keep checking changes in e.g. this and this file.
You cannot without rooting your phone. I am not sure why this is the case, especially where location services are required now for viewing scan results since SdkVersion 23+ .
This issue is very revealing.
https://code.google.com/p/android/issues/detail?id=185370
The best answer is application developers are being forced to crowd source network location for their google overlords. Notice, the OS itself has no problem displaying scan results without location services on.
Revealing that there is no way to turn on location services without GPS where location services are inactive. This two step shuffle is a major quality of experience issue for users. Turn location services on, then turn GPS off.
Watch this space, more lock downs on the way.
I need to access to programmatically turn ON & OFF Power Saving Mode in android.
Is there any API for this ? As I can see Power Saving mode option is not available
for all mobile devices, is there any code to check these settings availability and if available then turn it ON and OFF ?
Also is there any work around ?
If you want to test the Power Saving Mode (PSM):
The following works with Android Studio 3.4.1 and a Google Pixel 2 XL phone:
$ adb shell dumpsys battery unplug # a charging device cannot enter PSM
$ adb shell settings put global low_power 1 # enter Power Saving Mode (PSM)
This only works on devices that has the standard Power Saving Mode, like Samsung's, but not Huawei's, for example. On some Samsung devices, this setting is stored in global setting keyed psm_switch, as mentioned above.
You can leave PSM by setting low_power to 0 or by enabling charging again with:
$ adb shell dumpsys battery reset
Source: https://developer.android.com/training/monitoring-device-state/doze-standby#testing_doze_and_app_standby
It's bad practice to control the user's device settings from your app.
Rather consider suspending background services when battery levels are low.
Or to notify the user of low battery power and advising the user to switch of unnecessary settings like Bluetooth or WiFi.
Most devices won't have a built-in power-saver API, so you could try to make your own power-saver mode by turning off the bluetooth if it's on, the wi-fi, etc, deactivating location in settings, or checking for other power-eating options enabled.
There are no available API's to do this in android SDK. however You can control/save the power by Turning off Blutooth,Wi-Fi and other unused sevices inside your coding.
You can't set the power saving mode programmatically. Allowing this would be a bad idea.
On certain Samsung devices you can check if it is enabled:
final String result = Settings.System.getString(getContentResolver(), "psm_switch");
Log.v("Debug", "Powersaving active: " + TextUtils.equals(result, "1"));
See this for more info: https://stackoverflow.com/a/39296959/3600178
Does anybody meet this situation before? I force stop an app in the setting dialog in a Android device.
But when I use ps command via adb it is still displayed on the screen. But this process can't be get by the activityManager.getRunningAppProcesses(). This process even exists after I uninstall this app, It happened randomly.
Because I opened a port in my app using ServerSocket, the port can't be released after I stop my app.
The next time I tried to open my app, it failed to using this port again.
I am using a ASUS EeePad and this problem seems only happened on that device.
By the way, the process can't be get by any 3rd party tools such as process manager.
But it really exists with a pid when I use ps via adb shell to list all processes.
Since there is no code .. here are some hints :
1-do you close your connection ??? that will be the main reason to hold the port busy.
2- deal with socket more safely when your app going to pause , stop, or being destroyed .. you can override onpause,onstop, or ondesotry ..and free the port before closing the app.
3- make connection in a separated thread and implement a timer to check if you really need the port or not ( I mean if your app is alive or not ) and based on that kill/leave the thread (connection)
4- in worst cases .. if you have control over the other side of connection ( server, device .. ) try to make your solution more flexible like making range of ports to firstly check then use if they are available.
good luck