在启用互联网连接后,应用程序仍停留在启动画面

时间:2016-11-16 06:13:43

标签: android android-intent splash-screen

我设置我的启动活动以检查是否有活跃的互联网连接。如果没有互联网连接,我会设置一个警告对话框,弹出一个按钮以打开互联网设置。现在我面临的问题是,即使在启用互联网连接并进入bac后,它仍然停留在启动画面中。应用程序无法打开新活动。

SplashActivity.java

public class SplashActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    boolean mobileNwInfo;
    ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    try {     
          mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected();   
      }
    catch (NullPointerException e) {     
          mobileNwInfo = false;
      }
    if (!mobileNwInfo) {
        new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE)
                .setTitleText("Info ")
                .setCustomImage(R.mipmap.alert)
                .setContentText("No internet connection!")
                .setConfirmText("OPEN SETTINGS")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sDialog) {
                        sDialog.dismissWithAnimation();
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                    }
                }).show();
    }
    else
    {
      Splash();
    }
}

public void Splash()
{
    int SPLASH_TIME_OUT = 5000;
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {

            Intent i = new Intent(SplashActivity.this, Main2Activity.class);
            startActivity(i);
            finish();
        }
    }, SPLASH_TIME_OUT);
}
}

3 个答案:

答案 0 :(得分:2)

您必须在 onResume()中查看互联网状况。请尝试以下代码:

<form>
        <select id="countries" onchange="test()">
            <option value="France">France</option>
            <option value="Nigeria" >Nigeria</option>
        </select><br/>
        Student Role:
        <select id="student" onchange="window.student.role()">
            <option value="user">User</option>
            <option value="Admin" >Admin</option>
        </select>
</form>

答案 1 :(得分:1)

只需将此代码粘贴到onStart()中即可删除其他内容     阻止来自onCreate(Bundle savedInstanceState)

void onStart()
{
    boolean mobileNwInfo;
    ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    try { 
           mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected(); 
        }catch (NullPointerException e) { mobileNwInfo = false; }


if (mobileNwInfo) {

  Splash();
}

}

答案 2 :(得分:0)

public class SplashActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

boolean mobileNwInfo;
ConnectivityManager conxMgr = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
try { 
   mobileNwInfo = conxMgr.getActiveNetworkInfo().isConnected(); 
    }
catch (NullPointerException e) { mobileNwInfo = false; }
if (!mobileNwInfo) {
    new SweetAlertDialog(this, SweetAlertDialog.WARNING _TYPE)
            .setTitleText("Info ")
            .setCustomImage(R.mipmap.alert)
            .setContentText("No internet connection!")
            .setConfirmText("OPEN SETTINGS")
            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sDialog) {
                    sDialog.dismissWithAnimation();
                    startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                }
            })
            .show();
}
else
{
  Splash();
}


}

  public void Splash()
 {
int SPLASH_TIME_OUT = 5000;
new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {

        Intent i = new Intent(SplashActivity.this, Main2Activity.class);
        startActivity(i);
        finish();
    }
}, SPLASH_TIME_OUT);
}

  }

在Manifest.xml中添加

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />