獲取手機公網ip
@SuppressLint("HandlerLeak")
public static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bundle data = msg.getData();
s = data.getString("s");
setS(s);
}
};
public static String getS() {
return s;
}
public static void setS(String s) {
AndroidIp.s = s;
}
private static String s;
public static String GetNetIp() {
URL infoUrl = null;
InputStream inStream = null;
String ipLine = "";
HttpURLConnection httpConnection = null;
try {
infoUrl = new URL("http://city.ip138.com/ip2city.asp");
URLConnection connection = infoUrl.openConnection();
httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inStream, "utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
Pattern pattern = Pattern
.compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");
Matcher matcher = pattern.matcher(strber.toString());
if (matcher.find()) {
ipLine = matcher.group();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inStream!=null){
inStream.close();
}
httpConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return ipLine;
}
//獲取手機運營商
public static? int getOperators(Context context) {
// 移動設備網絡代碼(英語:Mobile Network Code,MNC)是與移動設備國家代碼(Mobile Country Code,MCC)(也稱為“MCC /
// MNC”)相結合, 例如46000,前三位是MCC,后兩位是MNC 獲取手機服務商信息
int OperatorsName = 0;
TelephonyManager IMSI =? (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String subscriberId = IMSI.getSubscriberId();
// IMSI號前面3位460是國家,緊接著后面2位00 運營商代碼
System.out.println(IMSI);
if (subscriberId.startsWith("46000") || subscriberId.startsWith("46002") || subscriberId.startsWith("46007")) {
OperatorsName = 1;
} else if (subscriberId.startsWith("46001") || subscriberId.startsWith("46006")) {
OperatorsName = 2;
} else if (subscriberId.startsWith("46003") || subscriberId.startsWith("46005")) {
OperatorsName = 3;
}
return OperatorsName;
}
/**
* @param context Context
* @return true 表示網絡可用
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected())
{
// 當前網絡是連接的
if (info.getState() == NetworkInfo.State.CONNECTED)
{
// 當前所連接的網絡可用
return true;
}
}
}
return false;
}
/**
* 判斷WIFI網絡是否可用
*
* @param context
* @return
/
public static boolean isWifiConnected(Context context) {
if (context != null) {
// 獲取手機所有連接管理對象(包括對wi-fi,net等連接的管理)
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 獲取NetworkInfo對象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//判斷NetworkInfo對象是否為空 并且類型是否為WIFI
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
return networkInfo.isAvailable();
}? ? ? ? return false;
}
/**
* 判斷MOBILE網絡是否可用
*
* @param context
* @return
*/
public static boolean isMobileConnected(Context context) {
if (context != null) {
//獲取手機所有連接管理對象(包括對wi-fi,net等連接的管理)
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//獲取NetworkInfo對象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//判斷NetworkInfo對象是否為空 并且類型是否為MOBILE
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE)
return networkInfo.isAvailable();
}
return false;
}
/**
* 獲取當前網絡連接的類型信息
* 原生
*
* @param context
* @return
*/? ? public static int getConnectedType(Context context) {
if (context != null) {
//獲取手機所有連接管理對象
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//獲取NetworkInfo對象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
//返回NetworkInfo的類型
return networkInfo.getType();
}
}
return -1;
}
/**
* 獲取當前的網絡狀態 :沒有網絡-0:WIFI網絡1:4G網絡-4:3G網絡-3:2G網絡-2
* 自定義
*
* @param context
* @return
*/
public static int getAPNType(Context context) {
//結果返回值
int netType = 0;
//獲取手機所有連接管理對象
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//獲取NetworkInfo對象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//NetworkInfo對象為空 則代表沒有網絡
if (networkInfo == null) {
return netType;
}
//否則 NetworkInfo對象不為空 則獲取該networkInfo的類型
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_WIFI) {
//WIFI
netType = 1;
} else if (nType == ConnectivityManager.TYPE_MOBILE) {
int nSubType = networkInfo.getSubtype();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//3G? 聯通的3G為UMTS或HSDPA 電信的3G為EVDO
if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
&& !telephonyManager.isNetworkRoaming()) {
netType = 4;
} else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
|| nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
|| nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
&& !telephonyManager.isNetworkRoaming()) {
netType = 3;
//2G 移動和聯通的2G為GPRS或EGDE,電信的2G為CDMA
} else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
|| nSubType == TelephonyManager.NETWORK_TYPE_EDGE
|| nSubType == TelephonyManager.NETWORK_TYPE_CDMA
&& !telephonyManager.isNetworkRoaming()) {
netType = 2;
} else {
netType = 2;
}
}
return netType;
}
/**
* 判斷GPS是否打開
*ACCESS_FINE_LOCATION權限
* @param context
* @return
*/
public static boolean isGPSEnabled(Context context) {
//獲取手機所有連接LOCATION_SERVICE對象
LocationManager locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}