I want to disable the wifi when not connect to any network. It will be running in the background and disabled anytime when wifi are enable and not used. How can I do that? Please give me some advice. I have to use Foreground or Service/ Broadcast Receiver.
You may be interested in Tasker. It can automate basically every setting in Android.
You can make a profile State> Variable Value> %WIFI=on Action: If "Wifi Info" %WIFII !contains "CONNECTION" then Net> Toggle Wifi> Off
You can also do this in Java with a bit more work. I can provide details on that if you request.
And also if you're lazy you can download neat little apps that take care of it for you: Auto Wifi Off
Hope that helps
Related
I'm developing a tracking application and I need to prevent users from turning off the basic sensors used to determine the location. I can not modify the devices ROM or have root access (or at least it would be very desirable to had not), but I thought of using the Device Administration API to perform these functions through the Profile Owner or Device Owner modes. I'm basically looking for a method to block these functions in Android settings.
I'm unsure about whether this is possible and how to do it, I have not found examples in GitHub for applications that have implemented this. Could anyone give me a light, some example or specific documentation?
I tried to follow these three documentations, without success in finding a solution to this specific feature:
https://source.android.com/devices/tech/admin
https://developer.android.com/guide/topics/admin/device-admin
https://developers.google.com/android/management/introduction
This is an excerpt from what I've been trying:
setUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, true);
setUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, active);
setUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH, active);
private void setUserRestriction(String restriction, boolean disallow){
if (disallow) {
mDevicePolicyManager.addUserRestriction(mAdminComponentName, restriction);
} else {
mDevicePolicyManager.clearUserRestriction(mAdminComponentName,
restriction);
}
}
DISALLOW_CONFIG_BLUETOOTH
Added in API level 18
public static final String DISALLOW_CONFIG_BLUETOOTH
Specifies if a user is disallowed from configuring bluetooth. This does not restrict the user from turning bluetooth on or off. The default value is false.
This restriction doesn't prevent the user from using bluetooth. For disallowing usage of bluetooth completely on the device, use DISALLOW_BLUETOOTH.
This restriction has no effect in a managed profile.
Key for user restrictions.
Type: Boolean
You cannot prevent them from turning GPS, WIFI and Bluetooth off. What you can do is have an implementation as below or use this library.
https://github.com/KI-labs/gps-permission-checks-livedata
You can't, obviously for security reasons. If you want to achive something like that you'll probably need to modify the devices ROM. You should create a BroadcastReceiver and keep tracking Internet and Bluetooth connection changes, than you can properly handle it when user disconnect them pausing the service, showing a dialog, finishing the application or whatever you need to do.
It would be pretty weird if an app could have some control of user settings, imagine if you install an app, then suddently you can't disable wi-fi anymore until you unistall it. You can't do that for a good reason
Preventing bluetooth/wifi disconnection will also prevent usage of aircraft mode, that is a security issue bounded in the ROM and not overridable.
As suggested above your option is to monitor for wifi/bluetooth/gps deactivations and prompt the user with an alert.
By the way, GPS is not affected by aircraft mode, as it's a pure receiver and doesn't make active transmissions. In that case GPS will be always active and collecting informations (if active and the phone is not in power save mode, aka relying on wifi location). I suggest you to check if the user activated aircraft mode, in order to be less annoying with your alerts (air mode is mandatory in same situations, and should be considered "legal" by your application, and maybe less critical than an user voluntary disconnection
In simple words, You cannot, but you can listen to when wifi is enabled/connected, and you can prompt a dialog stating the reason.
This way it gives the user a more concise grip on what needs to be done.
Just a suggestion
I am trying to read the Preferred network type setting from the device. But nowhere android API's are available.
Use case:
Trying to read the Preferred network type and connected network type so that if the device has LTE enabled and the user is forcefully switched back to the lower network(3G,2G); then there should be a notification sent to the user.
I have checked the system setting code, But it's deprecated.
Settings.Secure.getString(getApplicationContext().getContentResolver(),
Settings.System.NETWORK_PREFERENCE);
Is there any alternate way to read the system secured settings(By reflection?).
And Also is it possible to write back the setting with the user permission?
Help is much appreciated.
I think the right code is:
Settings.Global.getString(context.getContentResolver(),Settings.Global.NETWORK_PREFERENCE)
How can I permanently disable mobile data connections in Android?
I know I can turn this on and off in the menu, but what I want is to permanently make it impossible.for anyone to turn this on, for example.by deleting a necessary file (similar to deleting IO80211Family.kext on a Mac).
At the same time, I want to retain the possibility to connect via WLAN.
Please provide an idiot level explanation.
"Please provide an idiot level explanation." I can not do this, because SO is not allowing it.
About solution to your problem,
Step-1 : Register receiver with android.net.conn.CONNECTIVITY_CHANGE action in manifest.
Step-2 : onReceive method of broadcastreceiver, check for the type of internet connection Data connection / WLAN.
Ref : Android: Internet connectivity change listener
Step-3 : Disable data connection programmatically
Ref : Enable/disable data connection in android programmatically
Does anyone know if its possible (programatically) to enable both 3G and WiFi to be used simultaneously, both receiving and sending packets?
I have seen various other questions on here, but with the Tethering ability inside Android now, I was wondering if this is a possibility? This has to be on a standard/stock device, and no modifications via root to the OS.
Thanks
Adam
I don't believe it is, as soon as wifi is turned on, 3g will automatically disconnect. As far as I know there is no way round it.
The only way you can achieve this is by using a APN name with the HttpConnection. This is possible in Java ME, Please visit this answer, however i have never tried it.
You need to check for the active connection, if not active first one the switch to the next connection.
public void shutup(){
SuDroid cmd = new SuDroid();
cmd.sh.runWaitFor("svc wifi enable");
cmd.sh.runWaitFor("svc data enable");
cmd.sh.runWaitFor("svc data prefer");
}
Using SuDroido
I want to be able to check the status of Droid's wireless and I was hoping that by reading /sys/class/net/tiwlan0/wireless/status I could find out if it was actually connected or not. I already know that I can check if my wireless is enabled/disabled but I want my app to notify the user if the wireless gets disconnected.
However, the Droid I have is not rooted and I do not have permission from the owner to root it and I need to know the type of the value (e.g. long, boolean, byte) before I can proceed.
Any help or suggestions about other ways I can go about this would be greatly appreciated!
Thanks
You should just be able to listen to the SUPPLICANT_STATE_CHANGED_ACTION broadcast. It will notify you of a change (scanning, association, disconnection, etc.) in the form of a SupplicantState object inside an extra.
Install terminal emulater, run it and type the following at the command line: "ifconfig -a". Read the IP address listed under tiwlan0.