//检查网络连接状态,Monitor network connections (Wi-Vi, GPRS, UMTS, etc.)
public static boolean checkNetWorkStatus(Context context){
boolean result;
ConnectivityManage cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if ( netinfo !=null && netinfo.isConnected() ) {
result=true;
Log.i(TAG, "The net was connected" );
}else{
result=false;
Log.i(TAG, "The net was bad!");
}
return result;
}
还要检查 URL 是否有效
public static boolean checkURL(String url){
boolean value=false;
try {
HttpURLConnection conn=(HttpURLConnection)new URL(url).openConnection();
int code=conn.getResponseCode();
System.out.println(">>>>>>>>>>>>>>>> "+code+" <<<<<<<<<<<<<<<<<<");
if(code!=200){
value=false;
}else{
value=true;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
--------------------------------------
android检查网络连接状态 还要检查 URL 是否有效
最新推荐文章于 2023-05-24 11:06:53 发布