{"id":918,"date":"2020-11-24T11:20:45","date_gmt":"2020-11-24T03:20:45","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=918"},"modified":"2023-05-04T16:14:17","modified_gmt":"2023-05-04T08:14:17","slug":"android-%e7%8d%b2%e5%8f%96%e6%89%8b%e6%a9%9f%e5%94%af%e4%b8%80%e7%a2%bc-%e7%8d%b2%e5%8f%96ip-%e7%8d%b2%e5%8f%96%e8%a3%bd%e9%80%a0%e5%95%86-%e7%8d%b2%e5%8f%96%e7%89%88%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/android-%e7%8d%b2%e5%8f%96%e6%89%8b%e6%a9%9f%e5%94%af%e4%b8%80%e7%a2%bc-%e7%8d%b2%e5%8f%96ip-%e7%8d%b2%e5%8f%96%e8%a3%bd%e9%80%a0%e5%95%86-%e7%8d%b2%e5%8f%96%e7%89%88%e6%9c%ac\/","title":{"rendered":"Util \u7372\u53d6\u624b\u6a5f\u552f\u4e00\u78bc \u7372\u53d6IP \u7372\u53d6\u88fd\u9020\u5546 \u7372\u53d6\u7248\u672c"},"content":{"rendered":"<h4>1.\u6dfb\u52a0\u6b0a\u9650<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;!--WIFI\u9700\u8981--&gt;\n&lt;uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\" \/&gt;\n&lt;!--IP\u9700\u8981--&gt;\n&lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;\n&lt;!--\u624b\u6a5f\u865f\u78bc\u9700\u8981--&gt;\n&lt;uses-permission android:name=\"android.permission.READ_PHONE_STATE\" \/&gt;\n&lt;uses-permission android:name=\"android.permission.READ_PHONE_NUMBERS\" \/&gt;\n<\/code><\/pre>\n<h4>2.\u88dd\u7f6e\u5de5\u5177\u985e<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">@Suppress(\"DEPRECATION\")\nobject DeviceUtil {\n\n    \/\/ANDROID_ID\u552f\u4e00\u78bc\n    fun getAndroidID(context: Context): String =\n        Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)\n\n    \/\/\u624b\u6a5f\u865f\u78bc\n    @SuppressLint(\"MissingPermission\")\n    fun getPhoneNumber(context: Context): String {\n        val subscriptionManager = context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager\n        val activeSubscriptionInfoList = subscriptionManager.activeSubscriptionInfoList\n        if (activeSubscriptionInfoList != null &amp;&amp; activeSubscriptionInfoList.size &gt; 0) {\n            for (subscriptionInfo in activeSubscriptionInfoList) {\n                return subscriptionInfo.number\n            }\n        } else {\n            return \"\u672a\u53d6\u5f97\"\n        }\n        return \"\"\n    }\n\n    \/\/\u96fb\u4fe1\u6f2b\u904a\n    fun getNetworkRoaming(context: Context) =\n        if ((context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).isNetworkRoaming) \"\u6f2b\u904a\u4e2d\" else \"\u975e\u6f2b\u904a\"\n\n    \/\/\u96fb\u4fe1\u570b\u5bb6\n    fun getNetworkCountryIso(context: Context): String =\n        (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).networkCountryIso\n\n    \/\/IP\n    fun getIP(context: Context): String {\n        val wifiIp = getWifiIP(context)\n        val GPRSIP = getGPRSIP()\n        var ip = \"0.0.0.0\"\n        if (wifiIp != 0) {\n            ip = intToIP(wifiIp)\n        } else if (!TextUtils.isEmpty(GPRSIP)) {\n            ip = GPRSIP\n        }\n        return ip\n    }\n\n    \/\/\u7372\u7db2\u8defIP\n    private fun getGPRSIP(): String {\n        try {\n            val en =\n                NetworkInterface.getNetworkInterfaces()\n            while (en.hasMoreElements()) {\n                val networkInterface = en.nextElement()\n                val addresses =\n                    networkInterface.inetAddresses\n                while (addresses.hasMoreElements()) {\n                    val inetAddress = addresses.nextElement()\n                    if (!inetAddress.isLoopbackAddress) {\n                        return inetAddress.hostAddress\n                    }\n                }\n            }\n        } catch (e: SocketException) {\n            e.printStackTrace()\n        }\n        return \"\"\n    }\n\n    \/\/wifi\u7684IP\n    private fun getWifiIP(context: Context) =\n        (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.ipAddress\n\n    \/\/IP\u8f49\u6210String\n    private fun intToIP(IPAddress: Int) =\n        (IPAddress and 0xFF).toString() + \".\" + (IPAddress shr 8 and 0xFF) + \".\" + (IPAddress shr 16 and 0xFF) + \".\" + (IPAddress shr 24 and 0xFF)\n\n\n    \/\/\u624b\u6a5f\u88fd\u9020\u5546&amp;\u578b\u865f\n    fun getManufacturerModel() = Build.MANUFACTURER + \" - \" + Build.MODEL\n\n\n    \/\/\u624b\u6a5fCPU\n    fun getCPU(): String = Build.SUPPORTED_ABIS[0]\n\n\n    \/\/\u624b\u6a5f\u7248\u672c\n    fun getVersionRelease(): String = Build.VERSION.RELEASE\n\n\n    \/\/\u7248\u672c\u540d\u7a31\n    fun getVersionName(context: Context): String =\n        context.packageManager.getPackageInfo(context.packageName, 0).versionName\n\n    \/\/\u7248\u672c\u865f\n    fun getVersionCode(context: Context) = if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.P) {\n        context.packageManager.getPackageInfo(\n            context.packageName,\n            0\n        ).longVersionCode.toString()\n    } else {\n        context.packageManager.getPackageInfo(context.packageName, 0).versionCode.toString()\n    }\n}\n<\/code><\/pre>\n<h4>3.\u6548\u679c\u5c55\u793a<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private lateinit var binding: ActivityMainBinding\n    private val readPermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {\n        Log.e(\"DeviceAndroid_ID\", DeviceUtil.getAndroidID(this))\n        Log.e(\"DevicePhoneNumber\", DeviceUtil.getPhoneNumber(this))\n        Log.e(\"DeviceNetworkRoaming\", DeviceUtil.getNetworkRoaming(this))\n        Log.e(\"DeviceNetworkCountry\", DeviceUtil.getNetworkCountryIso(this))\n        Log.e(\"DeviceIP\", DeviceUtil.getIP(this))\n        Log.e(\"DeviceManufacturerModel\", DeviceUtil.getManufacturerModel())\n        Log.e(\"DeviceCPU\", DeviceUtil.getCPU())\n        Log.e(\"DeviceVersionRelease\", DeviceUtil.getVersionRelease())\n        Log.e(\"DeviceVersionName\", DeviceUtil.getVersionName(this))\n        Log.e(\"DeviceVersionCode\", DeviceUtil.getVersionCode(this))\n    }\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        binding = ActivityMainBinding.inflate(layoutInflater)\n        setContentView(binding.root)\n\n        readPermission.launch(arrayOf(\n            android.Manifest.permission.READ_PHONE_STATE,\n            android.Manifest.permission.READ_PHONE_NUMBERS\n        ))\n    }\n\n}\n<\/code><\/pre>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2023\/05\/wp_editor_md_ba827d3b4941cd1cea328a2988e8cd36.jpg\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2023\/05\/wp_editor_md_ba827d3b4941cd1cea328a2988e8cd36.jpg\" alt=\"\" \/><\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u6dfb\u52a0\u6b0a\u9650 &lt;!&#8211;WIFI\u9700\u8981&#8211;&gt; &lt;uses-permission android: &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[9],"tags":[147,15,16],"class_list":["post-918","post","type-post","status-publish","format-standard","hentry","category-util","tag-device","tag-kotlin","tag-util"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=918"}],"version-history":[{"count":8,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/918\/revisions"}],"predecessor-version":[{"id":1916,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/918\/revisions\/1916"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}