android:onClick="checkInternet"
Java
public void checkInternet(View view) {
if (!isConnected(this)){
showCustomDialog();
}
Intent categoryIntent = new Intent(MainActivity.this,CategoryActivity.class);
startActivity(categoryIntent);
}
private boolean isConnected(MainActivity mainActivity) {
ConnectivityManager connectivityManager = (ConnectivityManager) mainActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiConnection = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileConnection = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifiConnection != null && wifiConnection.isConnected())
|| (mobileConnection != null && mobileConnection.isConnected())){
return true;
}else{
return false;
}
}
private void showCustomDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Please connect to the internet")
.setCancelable(false)
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
});
}
0 Comments