[英]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.