启用 Wi-Fi 后,它应该会打开一个新的 activity

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

我现在正在使用此代码来检查 Internet 连接是打开还是关闭。如果关闭,则会显示无线设置页面。所以,我想要的是,在我启用 wi-fi 连接后,它应该打开 SplashScreen 2 activity。这该怎么做?正如您将在下面看到的,没有 intent/action 要求打开新的 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();

}

}

您可以简单地将整个代码放在 onResume() 而不是 onCreate() 方法中,如下所示,

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

    }
}