获取本机号码:
TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
txtPhoneNumber.setText(phoneMgr.getLine1Number()); //txtPhoneNumber是一个EditText 用于显示手机号
获取本机内网IP:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
// 得到本机ip地址
不能直接获取本机上网IP,也就是外网IP(广域网IP),只能获取本机IP(局域网IP)。外网IP通过访问网络从服务器返回IP。根据IP获取城市地址也需要访问服务器返回城市地址信息
public class IpUtil { public static String getIpUrl = "http://www.cz88.net/ip/viewip778.aspx"; public static void getWebIp() { new Thread() { public void run() { String strForeignIP = ""; try { URL url = new URL(getIpUrl); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String s = ""; StringBuffer sb = new StringBuffer(""); while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); } br.close(); String webContent = ""; webContent = sb.toString(); String flagofForeignIPString = "IPMessage"; int startIP = webContent.indexOf(flagofForeignIPString) + flagofForeignIPString.length() + 2; int endIP = webContent.indexOf("</span>", startIP); strForeignIP = webContent.substring(startIP, endIP); } catch (Exception e) { e.printStackTrace(); } }; }.start(); } }
获取城市
public static final String sGetAddrUrl = "http://ip-api.com/json/"; public static void locateCityName(final String foreignIPString) { new Thread() { public void run() { try { HttpClient httpClient = new DefaultHttpClient(); String requestStr = sGetAddrUrl + foreignIPString; HttpGet request = new HttpGet(requestStr); HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { String cityName = EntityUtils.toString(response.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }; }.start(); }
判断有无网络连接:
检查网络类型:
- ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);
- //检查网络连接
- NetworkInfo info = mConnectivity.getActiveNetworkInfo();
- if (info == null || !mConnectivity.getBackgroundDataSetting()) {
- return false;
- }
- int netType = info.getType();
- int netSubtype = info.getSubtype();
- if (netType == ConnectivityManager.TYPE_WIFI) { //WIFI
- return info.isConnected();
- } else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { //MOBILE
- return info.isConnected();
- } else {
- return false;
- }
判断WiFi是否已连接
- /**
- * make true current connect service is wifi
- * @param mContext
- * @return
- */
- private static boolean isWifi(Context mContext) {
- ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
- if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
- return true;
- }
- return false;
- }
判断WiFi和移动流量是否已连接:
- public static boolean checkNetworkConnection(Context context)
- {
- final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
- final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- if(wifi.isAvailable()||mobile.isAvailable()) //getState()方法是查询是否连接了数据网络
- return true;
- else
- return false;
- }