In my application data comes from internet and I am trying to create a function that checks if a internet connection is available or not and if it isn\'t, it gives an alert
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){
Toast.makeText(context, "No Internet connection!", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
And you must add premission for accessing network state and Internet:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
public boolean isOnline()
{
ConnectivityManager connectionManager;
if(app_context!=null)
connectionManager = (ConnectivityManager) app_context.getSystemService(Context.CONNECTIVITY_SERVICE);
else
return false;
try
{
if (connectionManager.getActiveNetworkInfo().isConnected())
{
Log.e(THIS_FILE, "Communicator ....isConnected()");
return true;
}
else
{
Log.e(THIS_FILE, "Communicator ....isNotConnected()");
return false;
}
}
catch (NullPointerException e)
{
Log.e(THIS_FILE, "No Active Connection");
return false;
}
}
set permission in manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
try this
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
//connection is avlilable
}else{
//no connection
Toast toast = Toast.makeText(context, "No Internet Connection",
Toast.LENGTH_LONG);
toast.show();
}
and don't forget to add following permssions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Just Try this I am using for my professional app
import androidx.appcompat.app.AlertDialog;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (isOnline()) {
// Do you Stuff
} else {
try {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Error")
.setMessage("Internet not available, Cross check your internet connectivity and try again later...")
.setCancelable(false)
.setIcon(android.R.drawable.ic_dialog_alert)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).show();
} catch (Exception e) {
Log.d(Constants.TAG, "Show Dialog: " + e.getMessage());
}
}
}
public boolean isOnline() {
ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){
return false;
}
return true;
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
if(!isNetworkAvailable()){
//Toast.makeText(this, "No Internet Connection", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Closing the App")
.setMessage("No Internet Connection,check your settings")
.setPositiveButton("Close", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
write this code in your create method
if (internetConnection.hasConnection(BankAccount.this))
{
// call your methods
}
else
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
alertDialogBuilder
.setMessage("No internet connection on your device. Would you like to enable it?")
.setTitle("No Internet Connection")
.setCancelable(false)
.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();
}