If internet connection is false, it should wait to connect

时间:2015-09-14 15:21:36

标签: android android-intent android-internet

I have the code that checks the Internet connection with two actions. If the Internet connection is ON, the application starts one specific activity. If the Internet is disconnected, the application shows a message asking for user connecting to Internet to continue using the application normally.

So, I want this open a Wireless Setting Page and then when user enabling Wi-Fi, it should open a SplashScreen2 activity. This is very important.Just after ENABLE WI-FI or INTERNET CONNECTION.

But, my code instead of waiting the user connect to the Internet when if connection == false and then becomes true, the application just closes it, because I put finish();. I don't want my application finish it and I don't know how to do what I want to. I need some help.

There is my code:

public class Splash extends Activity  {


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

    ConnectivityManager connectivitymanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
    final boolean connected = networkinfo != null && networkinfo.isAvailable()
            && networkinfo.isConnected();
    Log.v("Network state : ", connected + "");

    Thread splashThread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (waited < 5000) {
                    sleep(100);
                    waited += 100;
                }
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                Looper.prepare();
                if (connected == false) {
                    Splash.this.runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            Toast toast = Toast.makeText(Splash.this, "You must connect to the Internet to continue", Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                            toast.show();

                        }
                    });
                    **finish();**

                } else {
                    finish();
                    startActivity(new Intent(Splash.this,
                            SplashScreen2.class));
                }
                Looper.loop();
            }
        }
    };
    splashThread.start();

    }
}

1 个答案:

答案 0 :(得分:3)

Instead of using finish(); in your code you can open up the settings page so that user can connect to the internet and when you open settings page do not finish your current activity like this:

Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);

In this way user can again try to go the next activity.

相关问题