簡單取得 Android 手機唯一識別碼 Hardware Id ( Not Advertising ID 非廣告ID )

  1. With App Lifecycle 應用程式重裝前都不會改變
  2. With Andoird OS Profile Lifecycle 作業系統Reset to factory前都不會改變
@SuppressLint("HardwareIds")
    private fun getHardwareId() : String {
        var output = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
        if(output == "") {
            val pref = this.getSharedPreferences("HardwareIds", Context.MODE_PRIVATE)
            output = pref.getString("hardware_id","").orEmpty()
            if(output == "") {
                output = UUID.randomUUID().toString()

                val editor = pref.edit()
                editor.putString("hardware_id", output)
                editor.apply()
            }
        }
        return output
    }

留言

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.