Util

Util 獲取手機唯一碼 獲取IP 獲取製造商 獲取版本

1.添加權限

<!--WIFI需要-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--IP需要-->
<uses-permission android:name="android.permission.INTERNET" />
<!--手機號碼需要-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />

2.裝置工具類

@Suppress("DEPRECATION")
object DeviceUtil {

    //ANDROID_ID唯一碼
    fun getAndroidID(context: Context): String =
        Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)

    //手機號碼
    @SuppressLint("MissingPermission")
    fun getPhoneNumber(context: Context): String {
        val subscriptionManager = context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
        val activeSubscriptionInfoList = subscriptionManager.activeSubscriptionInfoList
        if (activeSubscriptionInfoList != null && activeSubscriptionInfoList.size > 0) {
            for (subscriptionInfo in activeSubscriptionInfoList) {
                return subscriptionInfo.number
            }
        } else {
            return "未取得"
        }
        return ""
    }

    //電信漫遊
    fun getNetworkRoaming(context: Context) =
        if ((context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).isNetworkRoaming) "漫遊中" else "非漫遊"

    //電信國家
    fun getNetworkCountryIso(context: Context): String =
        (context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).networkCountryIso

    //IP
    fun getIP(context: Context): String {
        val wifiIp = getWifiIP(context)
        val GPRSIP = getGPRSIP()
        var ip = "0.0.0.0"
        if (wifiIp != 0) {
            ip = intToIP(wifiIp)
        } else if (!TextUtils.isEmpty(GPRSIP)) {
            ip = GPRSIP
        }
        return ip
    }

    //獲網路IP
    private fun getGPRSIP(): String {
        try {
            val en =
                NetworkInterface.getNetworkInterfaces()
            while (en.hasMoreElements()) {
                val networkInterface = en.nextElement()
                val addresses =
                    networkInterface.inetAddresses
                while (addresses.hasMoreElements()) {
                    val inetAddress = addresses.nextElement()
                    if (!inetAddress.isLoopbackAddress) {
                        return inetAddress.hostAddress
                    }
                }
            }
        } catch (e: SocketException) {
            e.printStackTrace()
        }
        return ""
    }

    //wifi的IP
    private fun getWifiIP(context: Context) =
        (context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.ipAddress

    //IP轉成String
    private fun intToIP(IPAddress: Int) =
        (IPAddress and 0xFF).toString() + "." + (IPAddress shr 8 and 0xFF) + "." + (IPAddress shr 16 and 0xFF) + "." + (IPAddress shr 24 and 0xFF)


    //手機製造商&型號
    fun getManufacturerModel() = Build.MANUFACTURER + " - " + Build.MODEL


    //手機CPU
    fun getCPU(): String = Build.SUPPORTED_ABIS[0]


    //手機版本
    fun getVersionRelease(): String = Build.VERSION.RELEASE


    //版本名稱
    fun getVersionName(context: Context): String =
        context.packageManager.getPackageInfo(context.packageName, 0).versionName

    //版本號
    fun getVersionCode(context: Context) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        context.packageManager.getPackageInfo(
            context.packageName,
            0
        ).longVersionCode.toString()
    } else {
        context.packageManager.getPackageInfo(context.packageName, 0).versionCode.toString()
    }
}

3.效果展示

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private val readPermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
        Log.e("DeviceAndroid_ID", DeviceUtil.getAndroidID(this))
        Log.e("DevicePhoneNumber", DeviceUtil.getPhoneNumber(this))
        Log.e("DeviceNetworkRoaming", DeviceUtil.getNetworkRoaming(this))
        Log.e("DeviceNetworkCountry", DeviceUtil.getNetworkCountryIso(this))
        Log.e("DeviceIP", DeviceUtil.getIP(this))
        Log.e("DeviceManufacturerModel", DeviceUtil.getManufacturerModel())
        Log.e("DeviceCPU", DeviceUtil.getCPU())
        Log.e("DeviceVersionRelease", DeviceUtil.getVersionRelease())
        Log.e("DeviceVersionName", DeviceUtil.getVersionName(this))
        Log.e("DeviceVersionCode", DeviceUtil.getVersionCode(this))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        readPermission.launch(arrayOf(
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.READ_PHONE_NUMBERS
        ))
    }

}

發表迴響