I have this code to check for internet connection.
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
//if network is active and have internet connection
if(info != null && info.isConnected()==true){
//Some code
}
//if network is inactive or doesn't have internet connection
else if(info == null || info.isConnected()==false){
Context context = getApplicationContext();
CharSequence text = " Bad internet connection.";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
When I start the program,everything works properly with turn on internet connection,but when I pull out the internet cable from my router and in my app still have turn on wifi the app get true with this (if(info != null && info.isConnected()==true)) and crash.I don't know why this code get true.
Use this for check condition of network:
if (info!=null && info.isAvailable() && info.isConnected()) {
return true;
} else {
return false;
}
}
Related
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();
}
}
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";
}
I have created an android application which user Http Post and Get calls to read and write data from web server. Nothing too flashy also. My phone/SIM has 4G. Every time the app tries to connect, thing go slow and most of the time the app crashes. So I changed settings to GSM only. Guess what? The app works much better. What could be the possible reasons for it?
Use the following method in order to detect all available type of networks at your client's disposal :
public static boolean checkNetworkRechability(Context context) {
Boolean bNetwork = false;
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
for (NetworkInfo networkInfo : connectivityManager.getAllNetworkInfo()) {
int netType = networkInfo.getType();
int netSubType = networkInfo.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
bNetwork = networkInfo.isConnected();
if (bNetwork == true)
break;
} else if (netType == ConnectivityManager.TYPE_MOBILE && netSubType != TelephonyManager.NETWORK_TYPE_UNKNOWN) {
bNetwork = networkInfo.isConnected();
if (bNetwork == true)
break;
} else {
bNetwork = false;
}
}
if (!bNetwork) {
Log.i(TAG, "You are not in network");
}
Log.i(TAG, "bNetwork : " + bNetwork);
return bNetwork;
}
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
}