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

时间:2016-06-05 19:49:53

标签: android android-studio splash-screen android-internet

我有一个启动画面,它运行正常,但现在我想运行一个功能来执行互联网检查并决定向用户发送哪个活动。

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);

        }
    }    
}

1 个答案:

答案 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);