Created
June 22, 2017 06:01
-
-
Save archie94/1e33c7944532a2b046250c3a67ec9e02 to your computer and use it in GitHub Desktop.
Android activity to check for internet connection. This code not only checks if device is connected to a router or not but also determines whether internet is accessible. This is done by trying to reach a service within a specified time span.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.arka.wifiinternet; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.TextView; | |
import java.io.IOException; | |
import java.lang.ref.WeakReference; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
public class NetworkStatusActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setImageResource(R.drawable.web_globe_solution); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
new NetworkStatus(view).execute(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
private class NetworkStatus extends AsyncTask<String, String, String> { | |
private WeakReference weakReference; | |
boolean ableToConnect = false; | |
boolean wifiConnected = false; | |
boolean cellularDataActive = false; | |
public NetworkStatus(View view) { | |
weakReference = new WeakReference(view); | |
} | |
@Override | |
protected String doInBackground(String... strings) { | |
ConnectivityManager connectivityManager | |
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); | |
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); | |
NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); | |
if (wifi.isConnectedOrConnecting ()) { | |
wifiConnected = true; | |
cellularDataActive = false; | |
} else if (mobile.isConnectedOrConnecting ()) { | |
wifiConnected = false; | |
cellularDataActive = true; | |
} else { | |
wifiConnected = false; | |
cellularDataActive = false; | |
} | |
if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) { | |
try { | |
URL url = new URL("http://google.com"); // keep your service here.. | |
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); | |
urlc.setConnectTimeout(2 * 1000); // set timeout here | |
urlc.connect(); | |
if (urlc.getResponseCode() == 200) { | |
Log.d("Connection", "Success !"); | |
ableToConnect = true; | |
} else { | |
ableToConnect = false; | |
} | |
} catch (MalformedURLException e1) { | |
ableToConnect = false; | |
} catch (IOException e) { | |
ableToConnect = false; | |
} | |
} | |
return ""; | |
} | |
@Override | |
protected void onPostExecute(String values) { | |
super.onPostExecute(values); | |
if (weakReference == null) { | |
return; | |
} | |
TextView textView = (TextView) findViewById(R.id.textView); | |
View view = (View) weakReference.get(); | |
if (ableToConnect) { | |
Snackbar.make(view, "Yo Internet :)", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
textView.setText("Yo Internet :)"); | |
} else { | |
Snackbar.make(view, "Not connected!! :(", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
textView.setText("Not connected!! :("); | |
} | |
if (wifiConnected) { | |
textView.setText("Connected to a Wifi"); | |
} else if (cellularDataActive) { | |
textView.setText("Cellular Data"); | |
} else { | |
textView.setText("Not Connected"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment