NetworkUtil

来源:互联网 发布:男灵剑士捏脸数据图 编辑:程序博客网 时间:2024/06/07 12:30
package com.ihandy.xgxsigndome.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


import android.content.Context;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Log;


import com.ihandy.core.util.android.SystemUtil;
import com.ihandy.xgxsigndome.AppConstants;


public class NetworkUtil {


/**
* 判断WIFI是否打开

* @param ctx
* @return
*/
public static boolean isOpenWifi(final Context ctx) {
final WifiManager wifi = (WifiManager) ctx
.getSystemService(Context.WIFI_SERVICE);
return wifi.isWifiEnabled();
}


public static boolean is3gDataActive(final Context ctx) {
final TelephonyManager tm = (TelephonyManager) ctx
.getSystemService(Context.TELEPHONY_SERVICE);
int state = tm.getDataState();
switch (state) {
case TelephonyManager.DATA_CONNECTING:
case TelephonyManager.DATA_CONNECTED:
return true;
case TelephonyManager.DATA_DISCONNECTED:
return false;
}
return false;
}


/**
* PING内网地址

* @param ctx
* @param LAN
* @return
*/
public static boolean ping(final Context ctx, String ip) {
// 使用Session连接一下服务端,如果能连上表示OK
URL url = null;
int code = 0;
try {
url = new URL(ip);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 * 1000);
conn.connect();
code = conn.getResponseCode();
Log.e("HTTP", code + "");
} catch (Exception e1) {
// TODO Auto-generated catch block
return false;
}
return code != 0;
}




/**
* 获取AndroidID

* @param ctx
* @return
*/
public static String getAndroidID(final Context ctx) {
String androidID = "";
if (ctx != null) {
androidID = Secure.getString(
ctx.getContentResolver(),Secure.ANDROID_ID);
}
return androidID;
}



/** 获得pad SN 码 */
public static String getDeviceSN() {
String sn_number = "";

String path = "/proc/sn_number";
 if(AppConstants.VERSION_RELEASE.startsWith("3")){      
  path = "/sdcard/sn.txt"; 
 }
 File file = new File(path);
 
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
sn_number = tempString;
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sn_number.trim();
}


/**
* 获取SIM

* @param ctx
* @return
*/
public static String getSIM(final Context ctx) {
String sim = null;
if (ctx != null) {
sim = SystemUtil.getSim(ctx);
}
return sim==null? "" : sim.trim();
}


}
原创粉丝点击