Check Network Coverage in Android before trying to send SMS - android

I want to create an application for Android smartphones that checks if the phone is in Airplane Mode. If it is, the application gets the phone off the Airplane mode and checks if there is any network connectivity to send a SMS. When I say network connectivity, I mean mobile phone network coverage to send the SMS, I don't want to check for internet connectivity. If there is network connectivity the application will try to send a SMS.
I have managed to do the Airplane Mode check and toggle, but I don't find a way to see if the phone is connected to the cellular network and if there is coverage. I found many examples that check for internet network connectivity, but is not what I need.
Is there any way to check if the phone is connected to the cellular network and if there is coverage to send SMS?
Thanks in advance for your help.

Use this to check the signal strength:
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener signalListener = new PhoneStateListener() {
public void onSignalStrengthChanged(int asu) {
//asu is the signal strength
}
};
telephonyManager.listen(signalListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

Related

Find available wifi connections in Android

I am trying to create a help wizard to recover from bad network connections in the app. One test case I hope to handle is the case where an end user has WiFi turned off, WiFi is available, and the mobile network is slower than the WiFi network. In this event, I want to be able to (1) discover the available WiFi network s, (2) find the WiFi network speed, (3) Compare its speed to the mobile network speed, (4) digest the user changes to the faster network.
For this to work, I need to know how to programmatically get information on available connections. Is that something we can do? If so, how can we tell what connections are available? Thanks in advance.
Task-1: Discover the available WiFi network
This can be done by getting WifiManager's instance from the System.
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getScanResults();
// The above is an async call and will results are available System will broadcast `SCAN_RESULTS_AVAILABLE` intent and you need to set a `BroadCastReceiver` for it.
// And get the results like this
List<ScanResult> results = wifiManager.getScanResults();
Task-2&3: find the network speed
This link gives an answer to your question about how to get network speeds of wifi and mobile network
Wifi:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
In case of mobile it should work:
TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength();
cellSignalStrengthGsm.getDbm();
Then You should compare this signal levels and if WIFI signal is better keep it turn on, but if mobile is better disconnect wifi
Task-4: Switching to the option with higher speed
In Android by default, if wifi is on and connected then your mobile network won't be used. Hence to use mobile data you must either disconnect from all available wifi-networks or switch off the wifi.
I will also suggest you to read link this, this and this for getting more information on how to get connection speed.

Detect nearby wifi without connecting to it

I want my app to "do something" when a wifi is nearby. But the phone doesn't connect to it. I don't have the password. For example, if the wifi University0001 is reachable I would like to silent my phone but I don't have the password to University0001.
Is there a way to have a broadcast receiver that triggers when new (not connected) ssid are around?
I would not like to have a "timer" to periodically check new SSIDs. I am asking for a receiver to trigger when new networks are around and maybe then check for the one I want.
I hope to be clear.
Thanks
Is there a way to have a broadcast receiver that triggers when new (not connected) ssid are around?
yes there is.
if your WIFI is on, and you are not currently connected to a WIFI network - the system periodically scan for available access points. when it will detect new visible access point it will send the SCAN_RESULTS_AVAILABLE_ACTION broadcast.
you can register to that broadcast, and when it receives - get from WifiManager the scan results:
#Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiManager (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> scanResults = wifiManager.getScanResults();
}
if you are already connected to a WIFI network - then you won't have any choice but to trigger the scan yourself periodically...
be careful:
performing from this receiver long operations (such network requests..) any time it receives is a bad approach - you'll drain the users battery very fast.

Android WifiLock WIFI_MODE_SCAN_ONLY not working

I'm trying to block the wifi connections. I want my application to turn on the wifi, but does not connect to any network that is already stored on the smartphone. But even after I use the SCAN_ONLY mode, he continues to connect to networks that already "know".
.....
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
.....
wifiManager.setWifiEnabled(true);
WifiLock scanOnly = wifiManager.createWifiLock(WifiManager.WIFI_MODE_SCAN_ONLY, "scanOnly");
scanOnly.acquire();
Already in despair i tried to disconnect after to make sure that the state is WIFI_STATE_ENABLED wifi. That the app can not connect for a few seconds, but after some time it connects to a network in the same ...
wifiManager.setWifiEnabled(true);
....
WHEN (WIFI STATE == WIFI_STATE_ENABLED)
{wifiManager.disconnect();
scanOnly.acquire();}
Can someone help me?
Tks
WifiLock not for this. Only for "Allows an application to keep the Wi-Fi radio awake". This does not forbid anyone to connect to wifi. Only says "I want to be able to do at least this in sleep mode".
You have to do what you said Peanut above: get the list of all the stored networks and then disable wifi in BroadcastReceiver.
#Override
public void onReceive(Context c, Intent intent) {
saveWifiList(mainWifi.getScanResults());
unregisterReceiver(this);
mainWifi.setWifiEnabled(false);
showWifiList();
}
One way would be to get the list of all the stored networks and then disable them. You might want to store their state somewhere so you can restore the initial state after you're done.

How to get WIFI to turn on during POWERUP?

For a long time my device would always auto start WIFI now it does not. Could someone show how to turn wifi on automatically when device boots? Also if my app sleeps sometimes on recovery wifi is gone? Show how to test if wifi is active and if not active how to turn it on programatically. Thanks
Use wifi manager class and use the following code to test wifi connectivity
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if(wifi.isWifiEnabled())
{
// write your code
}else{
wifi.setWifiEnabled(true);
}

How to check for phone network on Android devices

I know how to check if I have internet access (using the code from this post),but is it possible to check if a phone has telephone network access? For example someone might have access to the internet via Wifi but not have phone network access to send SMS or make calls.
In my case, while using a real device (Samsung Galaxy S), I am able to turn of my 3G network (then the phone will detect I am not connected to the internet), but I am still able to make phone calls and send SMS. I guess I must be using some other network...
How do I test whether the phone network is connected? Do I need the TelephonyManager?
Thankyou for your time.
Mel
Sure would you not just use this:
getNetworkType()
boolean hasNetwork = android.telephony.TelephonyManager.getNetworkType() != android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;
// True if the phone is connected to some type of network i.e. has signal
The above answer did not work for one of my app users - he was connected to the Network but his NetworkType was unknown. Screenshot: http://dl.dropbox.com/u/5072192/SC20130317-161413.png
I am instead checking for the NetworkOperator field. (From the first answer here What is the correct way of checking for mobile network available (no data connection))
public static boolean isMobileAvailable(Context acontext) {
TelephonyManager tel = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return (tel.getNetworkOperator() != null && !tel.getNetworkOperator().equals(""));
}

Categories

Resources