How to change the activity if there is no internet connection in android studio (java)

 Do you want to check the internet connection on your android application?

This is how you do it...

  • STEP1
Add these permissions in the manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>   


  • STEP2
Copy paste these methods in the relevant java class file 


private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private void noNetwork(){
    boolean check=isNetworkAvailable();
    if (check==false){
        changeactivityno();
    }
    return;
}
public void changeactivityno(){
        Intent intent=new Intent (this, nonetwork.class);
        startActivity(intent);
    }

  • STEP3
Call the function noNetwork() where you need to check the internet connection. 

example; I'm checking the internet connection when im initializing the activity


  • STEP4
Create a new activity called nonetwork and design it as you prefer to handle the no internet connection error 

example: 


note: you can add pictures, text, etc.. 






Comments

Popular posts from this blog

About

How to change/update the initial app configuration on google play console