I want to check whether Internet connection is turned on or not when app starts. It should allow to start if internet is connected. Else display a error message and direct user to settings. Message need to be displayed until Internet connection is turned on.
Already I finished connection check function and alert display function. How could I listen whether internet is turned on or not after user directed to settings
connection check function is,
public boolean connectionIsAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
return true;
}
return false;
}
and alert display,
public void displayAlertDialog(final Context context){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context, AlertDialog.THEME_TRADITIONAL);
alertDialogBuilder.setMessage("Would you like to enable it?")
.setTitle("No Internet Connection")
.setPositiveButton(" Enable Internet ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(dialogIntent);
}
});
alertDialogBuilder.setNegativeButton(" Cancel ", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
In my onCreate I check like,
if(connectionIsAvailable(getApplicationContext())) {
welcomeThread.start();
} else {
displayAlertDialog(LoadingScreenActivity.this);
}
Help me how could I listen whether internet is turned on or not after user directed to settings. Thanks in advance.