If you go through HTC phones, they have an option for mobile data network setting to "Always enable mobile data". As I understand, it helps to maintain a TCP connection. I want to implement the same functionality, how to implement it ?
as I think, need to use wakelock?
WakeLocks are a Power Manager system Service feature, available to your
applications to control the power state of the host device.
You cannot change the connection (Mobile or wifi). But you can listen the connection instantly and let your application works on only mobile data.
Related
i'm making and IOT app with Flutter, and i have to connecto to an API. There's a way to force the app to use 4g (or any kind of phone network), instead wifi to make these calls?, any kind of library or so?.
Thanks in advance!
This is something that should be avoided in a production app due to cellular and data fees. Also, there is no system in place to choose between your phone's available connections when making network requests.
I have an app that needs to connect to a hardware via Wi-Fi. For this purpose, the hardware offers an offline Wi-Fi Hotspot. The hotspot's DHCP doesn't give us a gateway or DNS so we know right away, that it's for offline use only.
The end user will be at work and have to stay connected to the Wi-Fi the whole time in order to receive sensor data and alerts if something is wrong with the data. But at the same time, we don't want to force the end user to be offline with his mobile device. He should be able to use his mobile data for our and all other apps he has.
I though I can control this by calling android.net.ConnectivityManager.requestNetwork. But it doesn't seem to work reliably. Will calling requestNetwork in my app influence if all the other apps have access to mobile data?
Is this even something that we can control? I tested this on multiple devices with different manufacturers and android versions and they all behave differently.
E.g. Pixel 3a works fine. After some time I get a notification regarding the offline Wi-Fi. If I ignore it, it keeps working. If I press "yes" on it, I am offline and connected to the Wi-Fi.
Huawei P20 Lite seems to fire a OnLost event for my mobile data NetworkCallback every time I request it. Like its trying to cancel the mobile connection every time I reconnect because I'm in the wifi. Some devices offer a "Smart Switch" (Samsung) or "Wi-Fi+" (Huawei) mode which isn't on per default. And it seems like a hassle to ask the user to turn it on, since they might not want to use it in different scenarios.
Here are some examples for the different devices:
When using requestNetwork() to stick to an offline wifi network, you need an instance of ConnectivityManager. You can get it as follows:
final ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
This method returns the OS's connectivity manager. Therefore other apps will also be impacted by the "sticky" mode you've put the ConnectivityManager in.
Unfortunately I don't think it's possible for an app to force OS to stay connected to an offline Wifi network, and at the same time keep using mobile data.
After trying this out on various devices, I can say, that it is not possible to achive the desired behaviour consinstently on all devices.
Pretty much all the devices, behave differently. The OS-version is not really a factor. It depends more on how the vendors handle the case, that a user is connected to an offline wifi.
Based on the device, it might be possible to stay connected to offline wifi and use mobile data at the same time for all apps. But the OS can automatically disconnect you or lock you into the offline-wifi and disable mobile data. Maybe it does it because the user selected an option in the system dialogs as shown above.
In the latter case, it is possible to force a mobile data connection with the ConnectivityManager for the own app. But it is not possible to make all the other apps use this mobile connection as well.
I would like to create Android background service application that prevents to shut down the 3G connection.
I will try to explain better, I've 4 Android tablets, and I need that my employees do not turn off the 3G connection.
Someone have an examples?
thanks beforehand
The only reason why a mobile data connection will shut down is because either:
The user turned on airplane mode, or
The user fussed around in Settings and screwed up an access point name or something
You cannot control either of these behaviors from an ordinary SDK app, except perhaps on a rooted device.
I am developing an application. In which, I want to disable the using Internet for some time, especially if he has homework. How can I disable the Internet ?
To enable/disable data connection programatically check this post
To disable / enable wi-fi refer this tutorial
You can set the "mobile data" and "wifi state" whithin your application as long as you have the right permissions.
Also, you can register system wide BroadcastListener to detect when the device is connected to the internet, and do whatever you consider needs to be done.
There are ways to do this using Airplane mode, or via system hooks but the user will still retain the power to connect back. If you disconnect it again, he can disable/remove your app.
Is there an absolute way to restrict the user? I don't believe so. For security reasons alone, Providing this ability to third party applications would be very dangerous.
How to force android to connect specifically defined wireless network
Is it possible?
User should able to connect defined work wi-fi or wifis,
but should not access any other networks like home.
Firstly for what you want to do that?For your application or the device?
If it's your application you can do restrictions about connection type and network etc.
But if you are talking about the device you cant achieve something like that unless you have the control of the whole system which is not possible.
One option would be to create an app or service that would pick up the:
NETWORK_STATE_CHANGED_ACTION Intent
And then in turn use the
WifiManager.getConnectionInfo()
to check whether this is a legitimate network, if not then just disconnect and notify the user.