Skip to content

Instantly share code, notes, and snippets.

@thanhcs94
Last active August 29, 2015 14:21
Show Gist options
  • Save thanhcs94/414d375d46904176eb8a to your computer and use it in GitHub Desktop.
Save thanhcs94/414d375d46904176eb8a to your computer and use it in GitHub Desktop.
Android_Tips
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectedDetector {
private Context _context;
public ConnectedDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity =
(ConnectivityManager) _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 true : internet connected
//retrun false : network error
return false;
}
}
@thanhcs94
Copy link
Author

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment