Search code examples
androidandroid-internet

After enable Wi-Fi, it should open a new activity


I'm using now this code that checks the Internet connection is on or off. If it is off, a wireless setting page is shown. So, what I want is, after I'll enable wi-fi connection, it should open SplashScreen 2 activity. How to do this? As you'll see below, there is no intent/action to ask to open the new activity.

public class Splash extends Activity {
static ConnectivityManager cm;
AlertDialog dailog;
AlertDialog.Builder build;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
// internet
build = new Builder(Splash.this); // connectivity
setContentView(R.layout.activity_splash);
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
        // there screen goes
        // to next screen
        // else shows
        // message
        .isConnectedOrConnecting()
        || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                .isConnectedOrConnecting()) {
    Log.e("cm value",
            ""
                    + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                            .isConnectedOrConnecting());
    Toast.makeText(Splash.this, "Internet is active", 2000).show();
    Thread mythread = new Thread() {
        public void run() {
            try {

                sleep(5000);

            } catch (Exception e) {
            } finally {
                Intent intent = new Intent(Splash.this,
                        SplashScreen2.class);
                startActivity(intent);
                finish();
            }
        }
    };
    mythread.start();
} else {

    build.setMessage("This application requires Internet connection.Would you connect to internet ?");
    build.setPositiveButton("Yes", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

  `Here the problem. There is no action after enable Wifi Connection. It should open SplashScreen2 activity`

        }
    });
    build.setNegativeButton("No", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            build.setMessage("Are sure you want to exit?");
            build.setPositiveButton("Yes", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });

        }
    });
    dailog = build.create();
    dailog.show();

}

}


Solution

  • You can simply put your entire code in onResume() instead of onCreate() method something like below,

    public class Splash extends Activity {
    static ConnectivityManager cm;
    AlertDialog dailog;
    AlertDialog.Builder build;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    }
    @Override
        public void onResume() {
        super.onResume();
        cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
        // internet
        build = new Builder(Splash.this); // connectivity
        if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
                // there screen goes
                // to next screen
                // else shows
                // message
                .isConnectedOrConnecting()
                || cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
                        .isConnectedOrConnecting()) {
            Log.e("cm value",
                    ""
                            + cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                                    .isConnectedOrConnecting());
            Toast.makeText(Splash.this, "Internet is active", 2000).show();
            Thread mythread = new Thread() {
                public void run() {
                    try {
    
                        sleep(5000);
    
                    } catch (Exception e) {
                    } finally {
                        Intent intent = new Intent(Splash.this,
                                SplashScreen2.class);
                        startActivity(intent);
                        finish();
                    }
                }
            };
            mythread.start();
        } else {
    
            build.setMessage("This application requires Internet connection.Would you connect to internet ?");
            build.setPositiveButton("Yes", new OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
    
                    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    
          `Here the problem. There is no action after enable Wifi Connection. It should open SplashScreen2 activity`
    
                }
            });
            build.setNegativeButton("No", new OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    build.setMessage("Are sure you want to exit?");
                    build.setPositiveButton("Yes", new OnClickListener() {
    
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            finish();
                        }
                    });
    
                }
            });
            dailog = build.create();
            dailog.show();
    
        }
    }