package ir.cristiansoft.checkinternet;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button btnCheckInternet;
Context context;
ConnectivityManager conMgr;
NetworkInfo networkInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
conMgr = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
networkInfo = conMgr.getActiveNetworkInfo();
btnCheckInternet = (Button) findViewById(R.id.button_checkinternet);
btnCheckInternet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( networkInfo == null || !networkInfo.isAvailable() || !networkInfo.isConnected() )
{
Toast.makeText(context , "Disconnected !!!!!!" , Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context , "Connected !" , Toast.LENGTH_LONG).show();
}
}
});
}
}