简体   繁体   English

当 Android Studio API 30 中没有互联网连接时,将用户带到离线活动屏幕

[英]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?注意:这不是重复资源我在 Stackoverflow 上查看了其他资源,其中许多只谈论如何检查连接,那些在 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?在 API 30 中实现这一目标的准确方法是什么?

You can use ConnectivityManager to check if the user has access to internet.您可以使用ConnectivityManager检查用户是否可以访问互联网。

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:然后您可以检查来自onCreate()的连接以将用户发送到另一个屏幕:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户离线时如何使用自定义的“无Internet连接可用”布局? - How to use a custom No Internet Connection Available layout when user is offline? 互联网离线时如何维护Channel API连接 - How to maintain Channel API connection when internet is offline ANDROID STUDIO:使用Camera API拍照->将此图片发送到另一个活动 - ANDROID STUDIO: Take picture with Camera API -> Send this picture to another activity Android studio-当互联网不可用时禁用下载按钮/活动 - Android studio- disable donwload button/Activity when internet not availabe 如果没有互联网连接,请将用户设置为设置 - Take user to settings if there's no internet connection 检查android studio中的互联网连接 - Checking internet connection in android studio 没有使用Firebase的互联网连接时如何获取对手用户的在线/离线状态 - How to get online/offline state of the opponent user when there is no internet connection using firebase 互联网连接可用时正在加载离线页面 - Offline Page is getting loaded when internet connection is available 在Android Studio中检测并处理缓慢的互联网连接 - Detect and handle slow internet connection in android studio Internet连接侦听器无法在Android Studio中工作 - Internet connection listener not working in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM