Nice programing

전화 시간이 아닌 "네트워크"시간 ( "네트워크 제공 값 사용"이라는 "자동"설정에서)을 어떻게 얻을 수 있습니까?

nicepro 2021. 1. 7. 21:18
반응형

전화 시간이 아닌 "네트워크"시간 ( "네트워크 제공 값 사용"이라는 "자동"설정에서)을 어떻게 얻을 수 있습니까?


내 응용 프로그램에서 날짜와 시간을 외부 소스에서 제공 한 것과 동기화하는 방법을 찾고 싶습니다.

실시간으로 5 분 정도 차이가 날 수 있기 때문에 전화 시간을 사용하고 싶지 않습니다. 추가 5 분 이하 = 10 분!

GPS 위성이나 네트워크 안테나의 시간 정보에 대해 들었습니다.

System.getCurrentTime으로 시도했지만 장치의 현재 값을 얻으므로 장치가 5 분 일찍 설정되면 잘못된 시간이 표시됩니다.

편집하다

짧은 질문을하려면 어떻게해야할까요?

여기에 이미지 설명 입력


나는 몰랐지만 흥미로운 질문을 발견했습니다. 그래서 안드로이드 코드를 파헤 쳤습니다 ... 감사합니다 오픈 소스 :)

표시되는 화면은 DateTimeSettings입니다. "네트워크 제공 값 사용"확인란은 공유 기본 설정 String KEY_AUTO_TIME = "auto_time";연결되어 있습니다.Settings.System.AUTO_TIME

이 설정은 mAutoTimeObserver2 개의 네트워크에서 호출 된 관찰에 의해 관찰됩니다 ServiceStateTracker: GsmServiceStateTrackerCdmaServiceStateTracker.

두 구현 모두 revertToNitz()설정이 참이 될 때 호출되는 메서드를 호출 합니다. 분명히 NITZ 는 캐리어 세계에서 NTP와 동일합니다.

결론 : 덕분에 이동 통신사가 제공 한 값으로 시간을 설정할 수 있습니다 revertToNitz(). 불행히도 네트워크 시간을 확보하는 메커니즘을 찾지 못했습니다. 정말로이 작업을 수행해야한다면 이러한 ServiceStateTracker구현 을 복사 하고 프레임 워크에서 제기 한 의도를 파악하고 (내가 생각하는) .NET Framework에 getter를 추가해야 mSavedTime합니다.


http://commons.apache.org/net/download_net.cgi 에서 라이브러리를 가져옵니다 .

//NTP server list: http://tf.nist.gov/tf-cgi/servers.cgi
public static final String TIME_SERVER = "time-a.nist.gov";

public static long getCurrentNetworkTime() {
    NTPUDPClient timeClient = new NTPUDPClient();
    InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
    TimeInfo timeInfo = timeClient.getTime(inetAddress);
    //long returnTime = timeInfo.getReturnTime();   //local device time
    long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();   //server time

    Date time = new Date(returnTime);
    Log.d(TAG, "Time from " + TIME_SERVER + ": " + time);

    return returnTime;
}

getReturnTime ()은 System.currentTimeMillis ()와 동일합니다.

getReceiveTimeStamp () 또는 getTransmitTimeStamp () 메서드를 사용해야합니다.

시스템 시간을 1 시간 전으로 설정하면 차이를 확인할 수 있습니다.

local time :
System.currentTimeMillis()
timeInfo.getReturnTime()
timeInfo.getMessage().getOriginateTimeStamp().getTime()

NTP server time :
timeInfo.getMessage().getReceiveTimeStamp().getTime()
timeInfo.getMessage().getTransmitTimeStamp().getTime()

이 코드 스 니펫을 시도하십시오.

String timeSettings = android.provider.Settings.System.getString(
                this.getContentResolver(),
                android.provider.Settings.System.AUTO_TIME);
        if (timeSettings.contentEquals("0")) {
            android.provider.Settings.System.putString(
                    this.getContentResolver(),
                    android.provider.Settings.System.AUTO_TIME, "1");
        }
        Date now = new Date(System.currentTimeMillis());
        Log.d("Date", now.toString());

매니페스트에 권한을 추가해야합니다.

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

이것은 나를 위해 일하는 것 같았습니다.

LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
long networkTS = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();

Android 2.2 API (레벨 8)에서 작업


NITZ는 NTP의 한 형태이며 레이어 3 또는 NAS 레이어를 통해 모바일 장치로 전송됩니다. 일반적으로이 메시지는 GMM 정보로 표시되며 다음 정보를 포함합니다.

특정 이동 통신사는이를 지원하지 않으며 일부는이를 지원하고 잘못 설정합니다.

레이어 3 시그널링 메시지

시간 : 9 : 38 : 49.800

GMM 정보 3GPP TS 24.008 버전 12.12.0 Rel 12 (9.4.19)

M 프로토콜 판별 기 (16 진 데이터 : 8)

(0x8) Mobility Management message for GPRS services

M Skip Indicator (hex data: 0) Value: 0 M Message Type (hex data: 21) Message number: 33

O Network time zone (hex data: 4680) Time Zone value: GMT+2:00 O Universal time and time zone (hex data: 47716070 70831580) Year: 17 Month: 06 Day: 07 Hour: 07 Minute :38 Second: 51 Time zone value: GMT+2:00 O Network Daylight Saving Time (hex data: 490100) Daylight Saving Time value: No adjustment

Layer 3 data: 08 21 46 80 47 71 60 70 70 83 15 80 49 01 00


Now you can get time for the current location but for this you have to set the system's persistent default time zone.setTimeZone(String timeZone) which can be get from

Calendar calendar = Calendar.getInstance();
 long now = calendar.getTimeInMillis();
 TimeZone current = calendar.getTimeZone();

setAutoTimeEnabled(boolean enabled)

Sets whether or not wall clock time should sync with automatic time updates from NTP.

TimeManager timeManager = TimeManager.getInstance();
 // Use 24-hour time
 timeManager.setTimeFormat(TimeManager.FORMAT_24);

 // Set clock time to noon
 Calendar calendar = Calendar.getInstance();
 calendar.set(Calendar.MILLISECOND, 0);
 calendar.set(Calendar.SECOND, 0);
 calendar.set(Calendar.MINUTE, 0);
 calendar.set(Calendar.HOUR_OF_DAY, 12);
 long timeStamp = calendar.getTimeInMillis();
 timeManager.setTime(timeStamp);

I was looking for that type of answer I read your answer but didn't satisfied and it was bit old. I found the new solution and share it. :)

For more information visit: https://developer.android.com/things/reference/com/google/android/things/device/TimeManager.html


the time signal is not built into network antennas: you have to use the NTP protocol in order to retrieve the time on a ntp server. there are plenty of ntp clients, available as standalone executables or libraries.

the gps signal does indeed include a precise time signal, which is available with any "fix".

however, if nor the network, nor the gps are available, your only choice is to resort on the time of the phone... your best solution would be to use a system wide setting to synchronize automatically the phone time to the gps or ntp time, then always use the time of the phone.

전화 시간은 정기적으로 동기화되는 경우 gps 또는 ntp 시간과 크게 다르지 않아야합니다. 또한 사용자가 시간을 동기화하도록 강요하는 것은 방해가 될 수 있으므로 사용자가 동기화를 수락하는지 물어 보는 것이 좋습니다. 마침내 그렇게 정확한 시간이 절대적으로 필요하다고 확신하십니까?

참조 URL : https://stackoverflow.com/questions/8049912/how-can-i-get-the-network-time-from-the-automatic-setting-called-use-netw

반응형