简体   繁体   中英

Take user to offline activity screen when no internet connection in Android Studio API 30

What is the best way to Redirect my user to another activity programmatically? Most of the codes I researched are using deprecated resources and some do not work again, they are also only work on button click?

How can I make it that when users arrive to my splash screen, if the system detects that they are not connected to the internet it automatically redirects to an activity specified by me.

NB: This is not a duplicate resource I have checked out other resource on Stackoverflow many of which only talk about how to check connection, what about those who which to make a redirect when the system checks that the internet is not connected in API 30?

I tried this which is not automatic, it only works on button click

  signupBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            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())) {
                Intent i = new Intent(StudentAppActivity.this, RegisterActivity.class);
                startActivity(i);
            }else{
                //no connection
                Intent i = new Intent(StudentAppActivity.this, Splash.class);
                startActivity(i);
            }
        }
    });

The problem I have with this code is that, assuming users launch my app for the first time without internet on, when they click on that button I specified here, the APP crashes, but when they turn on their data and open the app, then turn it off and click on the button, the code redirects well, but that will create a bad user experience assuming the user gets the app from a friend opens it without data and click on the button then the APP crash

What is the accurate way to achieve this in API 30?

You can use ConnectivityManager to check if the user has access to internet.

Code example:

boolean isConnectedToInternet() {

ConnectivityManager cm =
        (ConnectivityManager)SplashScreenActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();


return isConnected;

}

And then you can check the connection from onCreate() to send the user to another screen:

if (isConnectedToInternet()) {
   // user is connected to the internet
}else {
   // user is not connected to the internet
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM