How to "Restrict Background data" for selected app - android

I have a list of apps which consume high battery.
when I select one app, I want to "Restrict background data (Settings > Data usage > select app > Restrict background data)" for the selected app.
How Can I do this ?
Same question is asked at below link
Programatically toggle "Restrict Background Data"
And it says it can not be done.

You can run this command in the command line
svc data disable or svc data enable
You obviously need root to do that, like this:
Runtime.getRuntime().exec("echo svc data disable | su");
This should bring up root dialog if your device is rooted.

And it says it can not be done.
There is nothing in the Android SDK for this. Therefore, it is usually assumed to be impossible until proven otherwise. In this case, there is also a security aspect -- one app should not be able to control this setting for other apps, except perhaps on rooted devices.

You may try Reflection.I am not sure of it.

Related

Automatically configure Android settings?

At work, we have an Android-based infotainment system that we're constantly deploying new versions to, on a half-dozen different test benches. The deployment script does the moral equivalent of:
for apk in ${apk_files}; do
adb install -r ${apk]
done
After this, we need to manually execute the following steps:
Set the home app to be one of our just-installed applications (Always, not Just Once)
Become a developer, and enable the Stay Awake option
Select the Google TTS engine for text-to-speech functionality rather than Pico
Executing these steps after each deploy is a giant PITA. People often forget one or more steps, and leave the test bench in a non-working state. This results in a bunch of 'bogus' bug reports that waste everbody's time.
Is there some way (using adb, perhaps) that we can automate these steps?
You can disable other home apps with adb shell pm disable .... I don't think there's a command line option to set apps as default. I remember looking into this before and there was a "preferred application" XML file where this was stored. If you want to look into it, the magic happens in PackageManagerService.addPreferredActivityInternal(). Looks like it writes the data to a file on disk: package-restrictions.xml. I suppose it's possible you could figure out the format thereof and write the file (you'd need root).
This is controlled by a system settings, "stay_on_while_plugged_in". You can set it using adb shell settings system put ....
The TTS engine is stored in a secure setting, "tts_default_synth". You can see the value like,
$ adb shell settings get secure tts_default_synth com.svox.pico
com.svox.pico
And you can set it with adb shell settings put secure "tts_default_synth" <the value>.
I noticed that if the value was not been previously set, when you get the value using the settings command you get null and it's not listed in settings list, even though there is a default value. As of Android 6 (I think), settings are no longer in a DB but rather are stored in XML files in /data/system/users/0/settings_*.xml. You can see the values therein.

Programmatically enable/disable "Battery Saver" mode

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.

How is "Restrict Background Data" enforced in the Android stack?

Does anyone know just where in the Android stack the "Restrict Background Data" setting is enforced and how it is enforced? (I.e. apps in the background are not allowed to use the phone data network while running in the background - WIFI not affected.)
Thanks much,
Dawg
Android uses iptables rules as on any linux box to restrict data-access to certain users (every user downloaded app on android runs under its own unique user id).
If you wish to remove restriction for your app, you can remove the rule from a root shell.
iptables -F penalty_box -j REJECT
or on newer devices, as below:
iptables -F bw_penalty_box -j REJECT
However on a restart, Android will re-enforce the rule to iptables automatically. To prevent this, you need to remove <uid-policy ...> from "/data/system/netpolicy.xml". Global Restrict Background Data settings can be removed by setting "restrictBackgroundData" atribute to "false" on <policy-list ...> node.
Along with the netpolicy.xml changes, you must perform a reboot for your changes to take effect.
Note: Any of these operations may require to be run as root.

Wiping data on an actual phone

Ok, usually I use the emulator so I have just to check the "wipe-data" entry and that's it. If I'm using a phone how could I do that? Is there a way to have a "developer profile" on my phone so I don't lose my personal data?
I'm using my personal phone so....
If you go to Settings > Applications > Manage Applications > [particular application], there is an option to Clear Data. This will delete preferences etc. for the particular application. You can delete whole applications in the usual way through the menu, or with the adb uninstall [package name] command.
http://android-dls.com/wiki/index.php?title=ADB

Set Data roaming on/off

How could I programmatically set data roaming on/off in my android application ?
Apologies in advance for reopening a dead post but I have managed to achieve it by calling this executable:
su -c settings put global data_roaming0 1
Also to get the roaming setting for first SIM card:
su -c settings get global data_roaming0
If your app is signatureOrSystem/Privileged app (app resides in /system/priv-app) and your have valid android.permission.WRITE_SECURE_SETTINGS permission in system/etc/permissions. Then you can do it as below.
Enable :
Settings.Global.putInt(context.getContentResolver(), Settings.Global.DATA_ROAMING, 1)
Disable :
Settings.Global.putInt(context.getContentResolver(), Settings.Global.DATA_ROAMING, 0)
I hope that it's not possible to turn on data roaming programmatically as this would be a serious security issue from my point of view ...
Data roaming (i.e. UMTS data transfer via a foreign network) may result in a huge bill from your network provider - at least in europe.
If data roaming is currently on, then I think you can manipulate the Access Points Names in order to make it appear that a data service isn't available. See this post which also links to apndroid. You could browse their source and see the approach they have taken.
If data roaming has been set to off by the user, then this approach won't work. Though you could prompt users to turn it on as part of your install/setup process, which is the route apndroid take.
apndroid also provide an API for changing these kind of settings, which might be more convenient than reimplementing the same functionality.
On rooted devices when using su to enable data roaming, on multi sim devices the data roaming setting is sim specific.
So you need to get the sim number that is active for data calls
sim_num = settings get global multi_sim_data_call
and use this in the data_roaming + sim_num setting. EG sim_num 3
settings get global data_roaming3
if this is null then not multi sim device and use data_roaming otherwise use
settings put global data_roaming3
I was able to enable data roaming on my dual sim Motorola G8 without the need to root it via ADB. I'm posting here the procedure, because the phone has a bug that prevents data roaming from being enabled normally.
You need a computer and to install ADB installed
Enable developer mode and USB debugging in your phone
Connect the phone to the computer via USB and accept the debugging connection.
Start an "adb shell", be sure that it is correctly connected to your phone.
Issue the command
settings put global data_roaming1 1
to enable data roaming for SIM card 1, or
settings put global data_roaming2 1
to enable it for SIM 2
Note: if you do not have the USB cable, this can be done via wifi, but the pairing process is a bit complicated and version dependent.
If you do not have a computer, it may be possible to run the commands directly on the phone using a "local adb" app (there are a few on the play store), but I have not tested any.

Categories

Resources