【android】Android检查是否已经连接到网络
1 ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE); 2 boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting(); 3 boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting(); 4 if(wifi|internet){ 5 //执行相关操作 6 }else{ 7 Toast.makeText(getApplicationContext(), 8 "亲,网络连了么?", Toast.LENGTH_LONG) 9 .show(); 10 } 11 /* 12 另外需要权限 13 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 14 */
AndroidManifest.xml
1 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
by hacket