【问题标题】:Checking internet during Splash Screen在启动画面期间检查互联网
【发布时间】:2016-06-05 19:49:53
【问题描述】:

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

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

        }
    }    
}

【问题讨论】:

  • 你为什么不把MyConnectivityChecker合并成Splash
  • 我试过但没有成功:(
  • @KevinJhon 请看看我编辑的答案。
  • 真的,这是反模式。应用程序不应分为离线和在线活动或片段。您随时可能失去连接。
  • 为什么你不在用户所在的活动中设置监听器?使用BroadcastReceiver

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


【解决方案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);

【讨论】:

  • 方法verificaInternet()结果错误:“无法解析方法'verificaInternet()'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多