Android 獲取IPv4、IPv6、MAC、IMEI、AndroidID、設備序列號、當前時區、設備品牌型號

        最近在項目中需要上傳IPv4、IPv6、MAC、IMEI、AndroidID、設備序列號、當前時區、設備品牌型號信息給SDK,翻翻找找,東拼西湊,總算是完成了這個功能,分別記錄如下:

  1. 獲取IPv4:

        IPv4獲取分爲內網IP和外網IP,簡單說一下這兩者的區別,內網IP很好理解,就是給在一個路由器下掛着的內部網絡中的計算機分配的地址,一般都是192.168.0.***類似的;外網IP可以理解爲一個網絡羣體的IP,比如一個學校的網絡IP,就是這個學校總交換機(總路由器在外網中的地址)。以上理解可能有誤 ,僅供參考。

        獲取代碼:

/**
* 獲取外網IP地址
* @return
*/

public void GetNetIp() {
new Thread(){
	@Override
    public void run() {
		String line = "";
        URL infoUrl = null;
		InputStream inStream = null;
        try {
            infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
            URLConnection connection = infoUrl.openConnection();
            HttpURLConnection 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();
                while ((line = reader.readLine()) != null)
                    strber.append(line + "\n");
                inStream.close();
                // 從反饋的結果中提取出IP地址
                int start = strber.indexOf("{");
                int end = strber.indexOf("}");
                String json = strber.substring(start, end + 1);
                if (json != null) {
				    try {
                        JSONObject jsonObject = new JSONObject(json);
                        line = jsonObject.optString("cip");
                    } catch (JSONException e) {
                        e.printStackTrace();
	                }
			    }
                hostIp4 = line;
			}
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
}.start();
}

鑑於代碼複製過來排版混亂,我還是截圖吧。

2.獲取IPv6:

3.獲取MAC地址:

4.獲取IMEI:

5.獲取AndroidID:

6.獲取設備序列號:

String SerialNumber = Build.SERIAL;

7.獲取當前時區:

8.獲取設備品牌型號:

String deviceBrand = android.os.Build.BRAND; // 設備品牌
String deviceModel = android.os.Build.MODEL;  // 設備型號

這篇文章也算是比較難產的 ,由於某種原因,停頓了好久,終於算是把他發出來了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章