public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWang(); if(!initWang()) { Toast.makeText(this, "无网络", Toast.LENGTH_SHORT).show(); Intent in = new Intent("android.settings.WIRELESS_SETTINGS"); startActivity(in); return; } } private boolean initWang() { ConnectivityManager man = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = man.getActiveNetworkInfo(); if(info!=null&&info.isConnected()) { return true; } return false; } }