获取本机号码,本机IP

获取本机号码:

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.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>


// 得到本机ip地址
    public String getLocalHostIp()
    {
        String ipaddress = "";
        try
        {
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            // 遍历所用的网络接口
            while (en.hasMoreElements())
            {
                NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
                Enumeration<InetAddress> inet = nif.getInetAddresses();
                // 遍历每一个接口绑定的所有ip
                while (inet.hasMoreElements())
                {
                    InetAddress ip = inet.nextElement();
                    if (!ip.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(ip
                                    .getHostAddress()))
                    {
                        return ipaddress = "本机的ip是" + ":" + ip.getHostAddress();
                    }
                }

            }
        }
        catch (SocketException e)
        {
            Log.e("feige", "获取本地ip地址失败");
            e.printStackTrace();
        }
        return ipaddress;

    }

    // 得到本机Mac地址
    public String getLocalMac()
    {
        String mac = "";
        // 获取wifi管理器
        WifiManager wifiMng = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfor = wifiMng.getConnectionInfo();
        mac = "本机的mac地址是:" + wifiInfor.getMacAddress();
        return mac;
    }

不能直接获取本机上网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();

    }

判断有无网络连接:
 
 
  1. ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);   
  2. TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);   
  3. //检查网络连接   
  4. NetworkInfo info = mConnectivity.getActiveNetworkInfo();   
  5.   
  6. if (info == null || !mConnectivity.getBackgroundDataSetting()) {   
  7. return false;   
  8. }   
检查网络类型:
 
 
  1. int netType = info.getType();   
  2. int netSubtype = info.getSubtype();   
  3.   
  4. if (netType == ConnectivityManager.TYPE_WIFI) {  //WIFI  
  5.    return info.isConnected();   
  6. else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) {   //MOBILE  
  7.    return info.isConnected();   
  8. else {   
  9.    return false;   
  10. }   
判断WiFi是否已连接
 
 
  1. /** 
  2.  * make true current connect service is wifi 
  3.  * @param mContext 
  4.  * @return 
  5.  */  
  6. private static boolean isWifi(Context mContext) {  
  7.     ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);  
  8.     NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();  
  9.     if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {  
  10.         return true;  
  11.     }  
  12.     return false;  
  13. }  
判断WiFi和移动流量是否已连接:
 
 
  1. public static boolean checkNetworkConnection(Context context)  
  2.    {  
  3.        final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  4.   
  5.        final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
  6.        final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
  7.   
  8.        if(wifi.isAvailable()||mobile.isAvailable())  //getState()方法是查询是否连接了数据网络  
  9.            return true;  
  10.        else  
  11.            return false;  
  12.    }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值