gpt4 book ai didi

android - 在启动画面期间检查互联网

转载 作者:行者123 更新时间:2023-11-30 01:17:19 24 4
gpt4 key购买 nike

我有一个闪屏并且运行良好,但现在我想运行一个函数来执行互联网检查并决定向用户发送哪个 Activity 。

public class Splash extends Activity {

private static int tempo_splash = 1000;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Para o layout preencher toda tela do cel (remover a barra de tit.)

new Timer().schedule(new TimerTask() {

public void run() {
finish();

Intent intent = new Intent();
intent.setClass(Splash.this, MainActivity.class); //Chamando a classe splash e a principal (main)
startActivity(intent);
}
}, 2000);

}
}

这是我的 checkInternet 类(class):

public class MyConnectivityChecker extends AppCompatActivity {    

public void verificaInternet() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm.getActiveNetworkInfo()!= null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {

Intent i = new Intent(this, MainActivity.class);

} else {

Intent i = new Intent(this, CheckInternet.class);
startActivity(i);

}
}
}

最佳答案

你的代码可能是这样的

private Class verificaInternet() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

if (cm.getActiveNetworkInfo()!= null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return MainActivity.class;
} else {
return CheckInternet.class;
}
}

在您的 splashActivity 中添加上述方法,您的 timer 应该如下所示

new Timer().schedule(new TimerTask() {
@Override public void run() {
Intent intent = new Intent();
intent.setClass(Splash.this, verificaInternet()); //Chamando a classe splash e a principal (main)
startActivity(intent);
finish();//this should be after starting intent
}
}, 2000);

关于android - 在启动画面期间检查互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com