I can not find any plugin to get android cellular data (I want to know first if My connection is of type 2G, 3G or 4G then at least I want to get my signal strength and some other info but those 2 are very important).
Yes there is plugin available for check internet connection state in different parameter.like downloading and uploading data speed.
internet_speed_test
Like this,
Var unitText = unit == SpeedUnit.Kbps;
Related
The moment I get on a wifi connection, the cellular network is completely lost even though the cellular network indicator is definitely on.
This is my network request
val request = NetworkRequest.Builder().run {
addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
build()
}
connectivityManager.registerNetworkCallback(request, callback)
I've tried looking in the connectivityManager.allNetworks list and it's no where to be found. Only the wifi network is in there.
What's even weirder is there is one other cellular network that is always there. It does not have the same ID as my cellular network. There's no connection that can be made with it. It never shows up with registerNetworkCallback. The capabilities on it always include "valid" and "internet"
What am I seeing here? Why is my cellular network lost? What is this phantom cellular network?
targetSdkVersion: 29
Device: Galaxy S10 - Android 12
I figured this out.
If you call registerNetworkCallback the above will happen, but if you call requestNetwork with TRANSPORT_CELLULAR,
connectivityManager.requestNetwork(request, callback)
Android will keep the cellular network around. I was so confused because the documentation was so lacking. Once you do that, it will ask you to add the CHANGE_NETWORK_STATE permission.
After this step, the network is available, but you won't be able to make any request with it. You have to call
connectivityManager.bindProcessToNetwork(theCellularNetwork)
to get any connection.
After this is done, the cellular network can be used in tandem with the wifi network. You can even send some traffic to one and some to the other. If you use OkHttp like I do, you just bind the client with the network's socketFactory
val client = OkHttpClient().newBuilder().run {
socketFactory(network.socketFactory)
build()
}
client.newCall(
Request.Builder().url("https://example.com").build()
).execute().let {
Log.i(TAG, "Fetched ${it.body!!.string()}")
}
The cellular network isn't lost, but your app isn't allowed to use it. Once WiFi is connected, everything is forced to use that connection. The only exception to this rule is if your phone has a feature called "Dual Acceleration", which allows the cellular connection to stay active (and obviously, the user would have to enable that feature). Alternatively, you may have a setting in your phone's Developer Options called "Cellular Data Always Active", which will do the same thing.
But needless to say, you can't rely on either of those 2 features being enabled in a production environment. So, just assume that when WiFi is connected, that's the only connection that your app can use
in my application I am using work manager for periodic work. I am uploading files to server. I have one button on click of that button one dialog shown up and ask user - Which network you want to use while uploading file - 1. Wifi 2. Any
If user click on wifi I am uploading file after every 30 Min, If user click on Any I am uploading file after every 1 hr.
Following is my code for this:
1. If user select WIFI
PeriodicWorkRequest.Builder wifiWorkBuilder =
new PeriodicWorkRequest.Builder(FileUpload.class, 30,
TimeUnit.MINUTES)
.addTag("WIFIJOB1")
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.UNMETERED).build());
wifiWork = wifiWorkBuilder.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("wifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);
If User select Any:
PeriodicWorkRequest.Builder mobileDataWorkBuilder =
new PeriodicWorkRequest.Builder(FileUpload.class, 1,
TimeUnit.HOURS)
.addTag("MOBILEDATAJOB1")
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build());
mobileDataWork = mobileDataWorkBuilder.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("mobileDataJob", ExistingPeriodicWorkPolicy.REPLACE, mobileDataWork);
For any network it works perfectly and upload apk after every 1 hr. But if user select Wifi then here is problem -
If user connected to wifi of other mobile(say he is using hotspot) so here network is I guess consider as Metered network so it will not upload file. I just want to know our House or office network are by default are Unmetered network or not. If suppose its not fix (Means some are metered and some are unmetered) then using this code if user select wifi and user wifi is considered as metered then from his device file will never get uploaded.
Or should I create another task like :
PeriodicWorkRequest.Builder meteredwifiWorkBuilder =
new PeriodicWorkRequest.Builder(FileUpload.class, 45,
TimeUnit.MINUTES)
.addTag("METEREDWIFIJOB")
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.METERED).build());
wifiWork = wifiWorkBuilder.build();
WorkManager.getInstance().enqueueUniquePeriodicWork("meteredwifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);
So if user not connected to wifi file will be uploaded after every 1 hr, If connected to wifi (unmetered) file will be uploaded after every 30 min and if connected to metered wifi then file will be uploaded after every 45 min.
Is above logic make sense to create 3 sepearte task to upload file. Any suggestion will be appreciated. Thanks in advance
If all you care about is a presence of a network connection just use NetworkType.CONNECTED. If the file is very big, and could cost the user (as they will end up using an expensive data connection) you should use NetworkType.UNMETERED.
As of Android 12, the user can choose whether any particular Wi-Fi is metered or not in Settings / Connections / Wi-Fi / network settings / View more. So for example if they have a mobile hotspot on a limited data plan, they can set this one to Metered, but if they have unlimited Wi-Fi at home, they can set this one to Unmetered.
The default of "detect automatically" assigns Unmetered to most Wi-Fi networks, unless the router's DHCP server sets the value ANDROID_METERED in a Vendor Specific Option when assigning the IP address. Android's own "Mobile Hotspot" option does this, so if one Android device connects to the Wi-Fi hotspot of another Android device, it will default to metered, but other Wi-Fi will default to unmetered (and in both cases it can be overridden by the user as described above).
Mobile data networks are always treated as "metered" by Android (at least Android 9+) and I don't think there's any way to override this, other than rooting your device with a customised OS, or using a second device as a hotspot (and overriding its Wi-Fi to Unmetered).
I don't have any special insider knowledge, but I wouldn't be surprised if Google is contractually obliged to some carriers not to put an "unmetered mobile data" option into Android. Carriers that advertise "unlimited" data are rarely truly unlimited; usually it's just a word they use to attract new customers, but the small print gives a "fair use limit" that might be lower than you think. Some UK carriers have said "unlimited but no tethering", meaning the data must be used from the phone itself (and yes they can check the TCP hop count, plus on some phones the SIM card can instruct the phone to disable its tethering options); others have said "tethering allowed but there's a data limit on it"; obviously this is with the expectation that the phone itself will use less data than whatever you connect to its hotspot. Carriers have also been known to disallow app-update traffic in their firewalls, with the expectation that customers will also have access to Wi-Fi that lets them run the heavy updating.
Some apps ask the user whether they want to do something over Wi-Fi only (or unmetered Wi-Fi only), so you could let the user decide.
I have C++ code which can take a network info(struct ifaddrs) via netlink.
But I can't obtain next field:
network type - WIFI or MOBILE
mobile network type - like NETWORK_TYPE_CDMA, NETWORK_TYPE_LTE etc..
Any idea how can I get this info without Java(only C/C++) ?
How can I get info about mobile internet connection and assign it with network?
How can I programmatically check that internet access in a android device is only over Wi-Fi and not over Packet Data connections like UMTS EDGE or GPRS ?
if you want your app to only use one particular type of data connection, you can form if/else logic and use check for mobile-network-access and wifi-access and give command accordingly
you can refer to mobile-network-access and wifi-access questions of stackoverflow for check on connection availability
In my Android application I first get the users location using either GPS, the GSM network or a text value that is manually input via a Settings screen.
At first I used the Geocoder class to get the users locale with latitude and longitude provided by GPS or GSM but it seemed unreliable, now I used Google's web API to get the locale by making a HTTP request and parsing the XML document returned. This is using the following URL:
http://maps.googleapis.com/maps/api/geocode/xmllatlng=blahblahlat,blahblahlong&sensor=true
After getting the users locale I then send another HTTP request to free.worldweatheronline.com's weather API and parse the XML returned. With a stable internet connection the application runs fine, however my house has a rubbish signal and even worse mobile data connection.
I am aware there is a simple method to check if the device has a mobile data connection, however what I want to know is whether there is a way of measuring the signal strength as the problem arises when there is a connection, but it is too bad to successfully run. For example, if there is a way to get mobile data signal strength which returns a value from 0 (no signal) to 100 (full signal), I can then only carry out the location and weather retrieval if signal strength is above a certain amount.
Would it be best to just surround the location and weather retrieval code with a try/catch so it doesn't cause a runtime exception, allowing it to just fail gracefully then update when a better signal is acquired? Any ideas on how to accomplish this, or any other suggestions to make my app more friendly for users with bad signal?
Thanks in advance!
You can check it but remember to handle, in any case, drop of connections.
In my apps I had some problems while writing files or stuff like that with an open connection, so just remember that a fail may always occur (for example switching from gprs to wifi).
If you can easily check the signal strength I would do both.
Give a look at the SignalStrength class (never used it btw). Here a nice explanation.
You can check cell service signal strength by creating a PhoneStateListener and handle the onSignalStrengthChanged callback. For more info look at this question How to get cell service signal strength in Android?
EDIT :
You can pause PhoneStateListener by calling telephony.listen(listener, PhoneStateListener.LISTEN_NONE); to not receive updates when you don't need them.