Android 开发者 19小时前
TrustedTime API | 让应用时间更可靠
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

TrustedTime API 旨在为 Android 应用提供安全、可靠的时间源,避免用户篡改设备时间带来的问题。该 API 基于 Google 的基础设施,与设备本地时间设置无关,适用于日程安排、安全协议等多种场景。通过集成 TrustedTime API,开发者可以确保应用时间的准确性,提高数据一致性和安全性。

⏱️ TrustedTime API 利用 Google 的安全基础设施提供可信的时间戳,定期与 Google 服务器同步,确保时间源的准确性,避免了直接调用 NTP 服务器的低效。

🛡️ 准确的时间源对于数据一致性和安全性至关重要,用户修改设备时间可能导致数据不一致、安全漏洞、时间安排失准等问题,TrustedTime API 降低了这些风险。

⚙️ 集成 TrustedTime API 简单易行,开发者可以在应用生命周期的早期初始化 TrustedTimeClient,并在需要获取时间的地方调用相关方法,例如 getCurrentTimeInMillis()。

🌐 TrustedTime API 在 Android 5 (Lollipop) 及以上版本且搭载 Google Play 服务的设备上可用,但设备启动后需要连接互联网才能获取时间戳。

⚠️ TrustedTime API 提供了时间戳的估计误差,开发者可以根据误差评估时间戳的准确性。虽然提高了用户篡改时间的难度,但并不能完全保证安全性。

原创 Android 2025-06-13 17:32 北京

TrustedTime API 为您的应用提供可靠、安全的时间源。阅读本文,快速了解 TrustedTime API 的工作原理、用例和集成方法,让您的应用时间更精准,数据更可靠!

作者 / 软件工程师 Kanyinsola Fapohunda 和技术主管 Geoffrey Boullanger


准确的时间对于各种应用功能而言至关重要,无论是日程安排、事件管理、事务日志记录,还是安全协议。但因为用户可以更改设备的时间设置,所以开发者可能需要一种比设备本地系统时间更准确的时间源。鉴于此,我们推出了 TrustedTime API,它利用 Google 的基础设施提供可信的时间戳,独立于设备上可能被篡改的本地时间设置。



TrustedTime 的

工作原理是什么?



新的 API 利用 Google 的安全基础设施,为您的应用提供可信的时间源。TrustedTime 会定期将其时钟与 Google 的服务器进行同步,这些服务器可以访问高度准确的时间源,因此您无需在每次想要了解当前网络时间时都发出服务器请求。此外,我们还集成了一个独特的模型来计算设备的时钟漂移,这将在网络同步之间的时间可能不准确时通知您。



为什么准确的时间源很重要?



许多应用依赖设备的时钟来实现各种功能。然而,用户可能会有意或无意地更改其设备的时间设置,从而影响应用获取到的时间。这可能导致以下问题:




TrustedTime 用例



TrustedTime API 为提升应用的可靠性和安全性开辟了更多可能,其用例包括但不限于以下领域:




开始使用 TrustedTime API



TrustedTime API 基于 Google Play 服务而构建,大多数 Android 开发者可以无缝集成。


最简单的集成方式是在应用生命周期的早期初始化 TrustedTimeClient,例如在 Application 类的 onCreate() 方法中进行初始化。以下示例使用 Hilt 进行依赖项注入,使时间客户端在整个应用的组件中可用。


[可选] 设置依赖项注入


// TrustedTimeClientAccessor.kt

import com.google.android.gms.tasks.Task

import com.google.android.gms.time.TrustedTimeClient


interface TrustedTimeClientAccessor {

  fun createClient(): Task<TrustedTimeClient>

}


// TrustedTimeModule.kt

@Module

@InstallIn(SingletonComponent::class)

class TrustedTimeModule {

  @Provides

  fun provideTrustedTimeClientAccessor(

    @ApplicationContext context: Context

  ): TrustedTimeClientAccessor {

    return object : TrustedTimeClientAccessor {

      override fun createClient(): Task<TrustedTimeClient> {

        return TrustedTime.createClient(context)

      }

    }

  }

}


在应用生命周期的早期进行初始化


// TrustedTimeDemoApplication.kt

@HiltAndroidApp

class TrustedTimeDemoApplication : Application() {

  @Inject

  lateinit var trustedTimeClientAccessor: TrustedTimeClientAccessor

  var trustedTimeClient: TrustedTimeClient= null

    private set

  override fun onCreate() {

    super.onCreate()

    trustedTimeClientAccessor.createClient().addOnCompleteListener { task ->

      if (task.isSuccessful) {

        // Stash the client

        trustedTimeClient = task.result

      } else {

        // Handle error, maybe retry later

        val exception = task.exception

      }

    }

    // To use Kotlin Coroutine, you can use the await() method, 

    // see https://developers.google.com/android/guides/tasks#kotlin_coroutine for more info.

  }

}

NOTEIf you don't use dependency injection in your app. You can simply call

`TrustedTime.createClient(context)` instead of using a TrustedTimeClientAccessor.


在应用的任何位置使用 TrustedTimeClient


// Retrieve the TrustedTimeClient from your application class

  val myApp = applicationContext as TrustedTimeDemoApplication


  // In this example, System.currentTimeMillis() is used as a fallback if the

  // client is null (i.e. client creation task failed) or when there is no time

  // signal available. You may not want to do this if using the system clock is

  // not suitable for your use case.

  val currentTimeMillis =

    myApp.trustedTimeClient?.computeCurrentUnixEpochMillis()

        ?: System.currentTimeMillis()

  // trustedTimeClient.computeCurrentInstant() can be used if Instant is

  // preferred to long for Unix epoch times and you are able to use the APIs.


在诸如 Activity 之类的短生命周期组件中使用


@AndroidEntryPoint

class MainActivity : AppCompatActivity() {

  @Inject

  lateinit var trustedTimeAccessor: TrustedTimeAccessor


   private var trustedTimeClient: TrustedTimeClient? = null


  override fun onCreate(savedInstanceState: Bundle?) {

    super.onCreate(savedInstanceState)

    ...

    trustedTimeAccessor.createClient().addOnCompleteListener { task ->

      if (task.isSuccessful) {

          // Stash the client

          trustedTimeClient = task.result

        } else {

         // Handle error, maybe retry later or use another time source.

          val exception = task.exception

        }

    }

  }


  private fun getCurrentTimeInMillis() : Long? {

    return trustedTimeClient?.computeCurrentUnixEpochMillis()

  }

}



TrustedTime API 的

可用性和局限性



TrustedTime API 在所有运行 Android 5 (Lollipop) 及以上版本且搭载了 Google Play 服务的设备上均可使用。您需要添加依赖项 com.google.android.gms:play-services-time:16.0.1 (或更高版本) 以访问新的 API。使用此 API 不需要额外的权限。但是,设备启动后 TrustedTime 需要连接互联网才能提供时间戳。如果设备启动后未连接到互联网,TrustedTime API 将无法返回时间戳。


需要注意的是,由于温度、低电耗模式和电池电量等因素的影响,设备的内部时钟可能会出现漂移。TrustedTime 并不能防止这种漂移,但其 API 为每个时间戳提供了一个误差估计。您可以使用这个估计值来判断时间戳的准确性是否符合应用的要求。虽然 TrustedTime 提高了用户篡改应用访问时间的难度,但它并不能完全保证安全性。用户仍有可能通过高级技术来篡改设备的时间。



后续步骤



您可以访问 Android 开发者官方网站了解有关 TrustedTime API 的更多信息。也欢迎您持续关注 "Android 开发者" 微信公众号,及时了解更多开发技术和产品更新等资讯动态!





阅读原文

跳转微信打开

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Android TrustedTime API 时间同步 安全
相关文章