android - 同步Android和PC的系统时钟

标签 android tcp synchronization

我有一个 Android 应用程序通过 TCP 套接字发送到另一个 Android 手机或 PC,我想同步发送方和接收方时钟的时钟。我在 android 上尝试过 SystemClock.setCurrentTimeMillis,但即使使用 android.permission.SET_TIME 权限也无法正常工作。 还有别的办法吗?

最佳答案

不幸的是,您无法通过代码更改 Android 手机的时间,因为您没有设置 android.permission.SET_TIME 的特权。您只能在已获得 root 权限的手机上执行此操作。

那么,为什么不让您的两个应用程序都从像 internet time 这样的可信来源更新呢? . 也许使用 android sntp client

 //Sample usage:
 SntpClient client = new SntpClient();
 if (client.requestTime("time.nist.gov")) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() -    client.getNtpTimeReference();
  }

或者您可以做的是:将手机上的当前时间发送到电脑并设置电脑的时间,通过您正在使用的程序接收数据。 也可以在这里看看:set mobile time and date

关于android - 同步Android和PC的系统时钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12820899/

相关文章:

concurrency - 互斥与同步之间的区别?

java - 如何删除android系统栏(页脚栏)

Android - 多列 ListView

sockets - 套接字端口是否与常规端口相同

go - 如何为 golang tcp 服务器配置 GKE 负载均衡器?

mysql - PHP MYSQL Solr 自动更新

c - pthreads 没有串行执行

java - 无法使用 ProjectExecutionServices 创建 TaskExecuter 类型的服务

android - 在使用 Google Maps API 的 Android 项目上共享调试 key

unix 和 windows 上的 c++ socket udp 和 tcp