我有一个启动画面,它运行正常,但现在我想运行一个功能来执行互联网检查并决定向用户发送哪个活动。
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类:
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);
}
}
}
答案 0 :(得分:1)
您的code
看起来像这样
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);