1

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

    }
}
Holly Ikki
  • 199
  • 1
  • 10
  • possible duplicate of [Network listener Android](http://stackoverflow.com/questions/1783117/network-listener-android) – Viktor Yakunin Sep 14 '15 at 15:26
  • No, it is not duplicated question!! My problem is different!! – Holly Ikki Sep 14 '15 at 19:52
  • You live in async world, so you should react to async events, do not block your user it is not natural. For example you ordered a coffee would you stand still and wait for a coffee or would you check your email, text your friend and pick up your coffee when it is ready? – Viktor Yakunin Sep 15 '15 at 08:33

1 Answers1

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.

Aakash
  • 5,181
  • 5
  • 20
  • 37
  • I did exactly you said and then I erased "finish();" in my code. The setting wireless page is opened, but my application is stopped and finished at the same time. – Holly Ikki Sep 14 '15 at 16:08
  • there must be some exception, check your logcat – Aakash Sep 14 '15 at 16:10
  • } catch (InterruptedException e) { // do nothing – Holly Ikki Sep 14 '15 at 18:41
  • please post your logcat where you are getting the exception – Aakash Sep 14 '15 at 20:00
  • I'm trying doing this. I clicked on debug as android application....and when the app starts, the screen is frozen and nothing happens. So, my other question is, when the setting wireless page is opened and I enable wi-fi, for example, it should open splashscreen 2 activity. Your code above there is no action after enable wi-fi. What I should put to work this? – Holly Ikki Sep 14 '15 at 20:25
  • you can use startActivityForResult or you can check for internet connectivity in onResume of your splash screen – Aakash Sep 14 '15 at 20:36
  • also i can not tell the reason of your crash until i see the logcat. – Aakash Sep 14 '15 at 20:41
  • I found out the reason of the crash, because this activity was not declared as MAIN activity. So, when I put as a main activity, it worked well and fine. But, I don't want this as main activity, this is should continue as second activity opened after another SplashScreen. What should I do? – Holly Ikki Sep 15 '15 at 09:20
  • first thing the error is in BancoDictionary.onCreate() method on line 113. You should check this activity and if you don't get it, you can post it here – Aakash Sep 15 '15 at 15:00
  • BancoDictionary works fine. I launched this hundred times and shows the alertDialog perfectly (line 113). Splash works fine, but when this is main activity, I think when i remove splash to main activity and declare this as a normal activity, something is missing, maybe a parameter. But that's funny, the problem just occurs when internet is off, it show wireless settings page and then crashes. But when I set as main activity, this doesn't happen – Holly Ikki Sep 15 '15 at 15:18
  • have you tried adding finish() after startActivity in your else part. – Aakash Sep 15 '15 at 15:25
  • I solved my problem. I did some changes in my Main Activity - I erased "public void run" which was some code to start new activity, so i don't know why, but it was causing the crashing. Well, thank you for helping me and I marked your answer as a acceptable. – Holly Ikki Sep 15 '15 at 23:15