While doing OTP verification via Twiiter Digits, if the user is on VOLTE only connection and WIFI is connected on phone then DIGITS does not send OTP SMS. How can this be solved?
The possible solution could be to check whether the user is on a Packet only (PS) carrier and if the WIFI is connected then show a Alert to the User to turn OFF WIFI.
You can use the following code for this:-
private void check_wifi_for_volte(){
TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.getNetworkOperatorName();
Log.d(TAG,"carrierName:"+carrierName);
boolean isWifiActive =false;
ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
isWifiActive=true;
//Toast.makeText(this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
//Toast.makeText(this, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
}
} else {
// not connected to the internet
// can we show some toast not connected to internet
}
boolean isPSUser = carrierName.toLowerCase().contains(<PS-CARRIER>);
if(isPSUser && isWifiActive){
Toast.makeText(this, "If you are using PS Network. Turn OFF WIFI to get OTP SMS.", Toast.LENGTH_LONG).show();
}
}
Related
I'm currently writing an app where I want to detect which network type (2G, 3G or LTE) an Android device is currently connected to. I have tried using the following solutions:
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.getNetworkType()
and Connectivitymanager:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getSubtype()
Both methods work fine without WiFi connection but if connected to a WiFI TelephonyManager returns https://developer.android.com/reference/android/telephony/TelephonyManager#NETWORK_TYPE_IWLAN and ConnectivityManager returns 0 (UNKNOWN).
Is there any way to find out if a phone is connected to 2G, 3G or LTE even if it is connected to a WiFi network?
The issue was that in API-version 24 and forward getNetworkType() has been split in into two separate methods:
getDataNetworkType()
and
getVoiceNetworkType()
I added a check in my code to se if the returned type was IWLAN and if so use getVoiceNetworkType() instead.
if (tm.getNetworkType() != 18)
return tm.getNetworkType();
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED)
return tm.getVoiceNetworkType();
else
return -1;
You could try using getAllNetworks() from ConnectivityManager which tracks all active networks.
See here:
https://developer.android.com/reference/android/net/ConnectivityManager#getAllNetworks()
You can retrieve more detailed information on a certain returned network with getNetworkInfo()
See here:
https://developer.android.com/reference/android/net/ConnectivityManager#getNetworkInfo(android.net.Network)
call this method
public void checkNetworkType(){
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA)) {
Toast.makeText(this, "3G network type", Toast.LENGTH_LONG).show();
} else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPAP)) {
Toast.makeText(this, "4G network type", Toast.LENGTH_LONG).show();
} else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS)) {
Toast.makeText(this, "GPRS network type", Toast.LENGTH_LONG).show();
} else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE)) {
Toast.makeText(this, "2G network type", Toast.LENGTH_LONG).show();
}
}
For my app, I need to make sure the user is connected to wifi before contact with the server. I have found two methods to do so, but I am not sure if one suffices.
First I am adding this:
WifiManager wifiManager = (WifiManager) getActivity().getApplicationContext()
.getSystemService(WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
buildAlertNoWifi();
showProgressDialog(false, "");
return;
}
And then I am doing this:
ConnectivityManager cm = (ConnectivityManager) getActivity()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
return;
}
} else {
Toast.makeText(getContext(), "Make sure you connect to wifi.", Toast.LENGTH_LONG).show();
return;
}
So I was wondering if wifiManager.isWifiEnabled() returns whether the device is connected to a wifi or just has wifi turned on. And if so, is it enough to use it alone?
I believe WifiManager.isWifiEnabled() only checks if device's wifi is turned on. Please use NetworkInfo.isConnected() or NetworkInfo.isConnectedOrConnecting() to check if it's connected to any network.
Best Practice
public boolean isWifiConnected() {
NetworkInfo net = getActiveNetworkInfo();
return (isConnected(net) && net.getType() == TYPE_WIFI);
}
private NetworkInfo getActiveNetworkInfo() {
ConnectivityManager connManager = (ConnectivityManager)
Application.getContext()
.getSystemService(Application.CONNECTIVITY_SERVICE);
return connManager.getActiveNetworkInfo();
}
I believe this should work,
public boolean isWifiConnected()
{
ConnectivityManager cm = (ConnectivityManager)this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
return (cm != null) && (cm.getActiveNetworkInfo() != null) &&
(cm.getActiveNetworkInfo().getType() == 1);
}
I am developing android application using pjsua2.I am able to register,make calls and perform everything when there is internet connection but once the internet connection is lost and again connected to internet the connection is not established with the server. when I checked the logs it displayed Sip is not registered. even after internet connection is re-established.
Please some help to find what the mistake mght be ?
Thanks
You can use this piece of code
as described in this link
IpChangeParam changeParam = new IpChangeParam();
endpoint.handleIpChange(changeParam);
I found the same behavior when running in connection issues. I've ended registering to the android.net.conn.CONNECTIVITY_CHANGE to detect when internet status is changed and then using a method to detect the new status.Bellow is my implementation for the method:
public enum InternetStatus { WIFI, MOBILE, ROAMING, NO_INTERNET, UNKNOWN };
/**
* Get the internet status of the phone. The possible values are :
* +Not connected
* +Connected through WiFi
* +Connected to Mobile Carrier
* +Connected on Roaming
* #param context Context under which the app is running
* #return Returns the internet status as an enum value
*/
public static InternetStatus getInternetStatus(Context context)
{
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
//Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
return InternetStatus.WIFI;
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
// connected to the mobile provider's data plan
if(activeNetwork.isRoaming()) {
//Toast.makeText(context, activeNetwork.getTypeName() + " Roaming", Toast.LENGTH_SHORT).show(); // roaming
return InternetStatus.ROAMING;
}
else {
//Toast.makeText(context, activeNetwork.getTypeName() + " NOT Roaming", Toast.LENGTH_SHORT).show();
return InternetStatus.MOBILE;
}
}
} else {
// not connected to the internet
//Toast.makeText(context," NO Internet", Toast.LENGTH_SHORT).show();
return InternetStatus.NO_INTERNET;
}
return InternetStatus.UNKNOWN;
}
Once you get the internet connection you should access you SIP Account class and call
sipAccount.setRegistration(true);
This will trigger the library to send the registration message once again and everything should work properly.
Hope it helps.
Hello everyone I am having trouble on detecting the type of Internet connection that I get on my phone. So I use my code and what happens is that when I am using wifi the application returns to me the type Wifi but when I am using 3g connection I get a message saying the application has closed. Anyone can give me some advice ? Many thanks.
So here is my code.
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSDPA)) {
text = "3g";// for 3g HSDPA networktype will be return as
// per testing(real) in device with 3g enable data
// and speed will also matters to decide 3g network type
} else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_HSPAP)) {
text= "4g"; // /No specification for the 4g but from wiki
// i found(HSPAP used in 4g)
// http://goo.gl/bhtVT
} else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS)) {
text= "GPRS";
} else if ((tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE)) {
text= "2g";
}
else if ((info.getType() == ConnectivityManager.TYPE_WIFI)
) {
text= "WIFI";
}
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
And my permissions in the Manifest.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Any idea ?
Documentation says:
Returns details about the currently active default data network. When connected, this network is the default route for outgoing connections. You should always check isConnected() before initiating network traffic. This may return null when there is no default network.
http://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo%28%29
So maybe your info variable is NULL and causes a crash when you are not connected to any net.
change your code to:
else if ( info==null) { text= "no network"; }
else if ((info.getType() == ConnectivityManager.TYPE_WIFI)
) {
text= "WIFI";
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android - detect whether there is an Internet connection available
How to check network connection enable or disable in WIFI and 3G(data plan) in mobile?
In my cell when I run my app then this application is crashed because net connection is not there.so how to give the message that the connection is not there like. or on your wif
please help me
thank you
See below code.
ConnectivityManager conMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
// internet connection is wroking
} else {
return false;
// internet connection is not wroking
}
Use this method in your Activity and call this when you want to know whether the net is available or not.
public boolean isInternetOn(Context ctx) {
this.mContext = ctx;
ConnectivityManager Connect_Manager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
State connected = NetworkInfo.State.CONNECTED;
State connecting = NetworkInfo.State.CONNECTING;
State disconnected = NetworkInfo.State.DISCONNECTED;
State info0 = Connect_Manager.getNetworkInfo(0).getState();
State info1 = Connect_Manager.getNetworkInfo(1).getState();
// ARE WE CONNECTED TO THE NET
if (info0 == connected || info0 == connecting || info1 == connecting
|| info1 == connected) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
// Toast.makeText(this, connectionType + " connected",
// Toast.LENGTH_SHORT).show();
Log.d("Internet", "Connected");
return true;
} else if (info0 == disconnected || info1 == disconnected) {
Log.d("Internet", "DisConnected");
// System.out.println("Not Connected");
return false;
}
return false;
}
Now to check Internet Conncetion,
if (isInternetOn(this))
{
//Do stuff
}
else
{
//show alert
}