how to check net connection in android phone [duplicate] - android

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
}

Related

OTP Issue on TWITTER DIGITS for VOLTE Only Carrier

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();
}
}

android:how to detect network is connected to internet

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;
}
}

Android App crashes on 4G internet connection but works fine on GSM mode Only

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;
}

Android - How to check internet Access, not just connectivity to wifi? [duplicate]

This question already has answers here:
Detect if Android device has Internet connection
(16 answers)
Closed 9 years ago.
I tried the below code to check whether my mobile is connected to a wireless network and it works well when I want to know if my mobile is connected to the network, but it fails to give information about the internet access ... something like "Pinging" any website.
Actually I followed many links but still no answer, so I'll be so thankful if anybody can help.
Thanks in Advance.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast t = new Toast(getApplicationContext());
if (isInternetOn()) {
// INTERNET IS AVAILABLE, DO STUFF..
Toast.makeText(ConnectivityTestActivity.this,"Network is Available", Toast.LENGTH_LONG).show();
}
else {
// NO INTERNET AVAILABLE, DO STUFF..
Toast.makeText(ConnectivityTestActivity.this,"No Network Available", Toast.LENGTH_LONG).show();
}
}
public final boolean isInternetOn() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
return true;
} else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
return false;
}
return false;
}}
EDIT:
Follow below link it contains a great answer for Ping google server and get result
https://stackoverflow.com/a/16458623/1239911
Actually I used the same function isInternetOn() but I removed the connecting condition.
It had to check the status of connection if connected or not and if it is trying to connect. This didn't work for me, so I removed connecting status checking and then it worked.
Thanks for all replies.
public final boolean isInternetOn()
{
ConnectivityManager connec = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED )
{
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
return true;
}
else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED )
{
return false;
}
return false;
}
see the sample:
public static boolean isWifiEnabled() {
if ( !gWifiManager.isWifiEnabled()) {
if (mCanShowWifiToast) {
new Thread(mWifiToastControl).start();
G.gHandler.post(mNoWifiRunnable);
}
return false;
} else {
int linkspeed = gWifiManager.getConnectionInfo().getLinkSpeed();
if (linkspeed < 5) {
if (mCanShowWifiToast) {
new Thread(mWifiToastControl).start();
G.gHandler.post(mNoWifiRunnable);
}
return false;
}
}
return true;
}
You can use BroadcastReceivers that will trigger when the connection status change: there is a similar question with an answer: Broadcast intents for bluetooth, wifi and ringer mode

How to detect when WIFI is connected to internet?

I am building an Android app and I use the code below to detect whether there is a network connection. It works well and detects both mobile and WIFI networks.
My problem is how to detect an actual internet connection. The code below returns true when connected to WIFI however the WIFI might not necessarily be connected to the Internet.
The code
protected boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
return false;
}
} //end checkInterneConnection method
Thanks for your time.
Mel
You should try to reach an internet adress. Therefor you should check the InetAdress class and the method isReachable: http://developer.android.com/reference/java/net/InetAddress.html#isReachable%28int%29
This piece of code will check whether your device Internet conecction, If the signal is Poor it will show a Toast other wise not,
ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo Info = conMan.getActiveNetworkInfo();
if(Info == null){
Toast.makeText(RegisterActivity.this,"Network Connection Failed! ", Toast.LENGTH_SHORT).show();
}
You can try ping http://google.com or doing something like this to confirm it's ok to visit internet.
You should try this:
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager)
m_context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() ==
NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
And to check only wifi is simpler:
private boolean isWifiConnected() {
int WIFI_STATE = wifi.getWifiState();
if(WIFI_STATE == WifiManager.WIFI_STATE_ENABLED)
return true;
return false;
}
This code will really test the internet connection:
public static boolean isInternetAvailable() {
try {
InetAddress address = InetAddress.getByName("google.com");
return address.isReachable(2000); //This really tests if the ip, given by the url, is reachable
//return !address.equals(""); //This just tests if the IP is available but it could be taken in a previous request when internet was available
} catch (Exception e) {
Log.d("Internet check", "Unable to reach the url: "+url);
}
return false;
}

Categories

Resources