private boolean setTime(long timezone) { try { PermissionUtils.requestPermission(); } catch (Exception ex) { L.d("获取权限错误"); ex.printStackTrace(); } return SystemClock.setCurrentTimeMillis(timezone); }
private void onPollNetworkTime(int event) { // If Automatic time is not set, don't bother. if (!isAutomaticTimeRequested()) return; final long refTime = SystemClock.elapsedRealtime(); // If NITZ time was received less than mPollingIntervalMs time ago, // no need to sync to NTP. if (mNitzTimeSetTime != NOT_SET && refTime - mNitzTimeSetTime < mPollingIntervalMs) { resetAlarm(mPollingIntervalMs); return; } final long currentTime = System.currentTimeMillis(); if (DBG) Log.d(TAG, "System time = " + currentTime); // Get the NTP time if (mLastNtpFetchTime == NOT_SET || refTime >= mLastNtpFetchTime + mPollingIntervalMs || event == EVENT_AUTO_TIME_CHANGED) { if (DBG) Log.d(TAG, "Before Ntp fetch"); // force refresh NTP cache when outdated if (mTime.getCacheAge() >= mPollingIntervalMs) { mTime.forceRefresh(); } // only update when NTP time is fresh if (mTime.getCacheAge() < mPollingIntervalMs) { final long ntp = mTime.currentTimeMillis(); mTryAgainCounter = 0; // If the clock is more than N seconds off or this is the first time it's been // fetched since boot, set the current time. if (Math.abs(ntp - currentTime) > mTimeErrorThresholdMs || mLastNtpFetchTime == NOT_SET) { // Set the system time if (DBG && mLastNtpFetchTime == NOT_SET && Math.abs(ntp - currentTime) <= mTimeErrorThresholdMs) { Log.d(TAG, "For initial setup, rtc = " + currentTime); } if (DBG) Log.d(TAG, "Ntp time to be set = " + ntp); // Make sure we don't overflow, since it's going to be converted to an int if (ntp / 1000 < Integer.MAX_VALUE) { SystemClock.setCurrentTimeMillis(ntp); } } else { if (DBG) Log.d(TAG, "Ntp time is close enough = " + ntp); } mLastNtpFetchTime = SystemClock.elapsedRealtime(); } else { // Try again shortly mTryAgainCounter++; if (mTryAgainTimesMax < 0 || mTryAgainCounter <= mTryAgainTimesMax) { resetAlarm(mPollingIntervalShorterMs); } else { // Try much later mTryAgainCounter = 0; resetAlarm(mPollingIntervalMs); } return; } } resetAlarm(mPollingIntervalMs); }
public void setAdapter(SetConfigListViewAdapter.DataItem mDataItem) { if (SaveValue.getInstance(mContext).readValue(MenuConfigManager.AUTO_SYNC) == 1) { TVContent tv = TVContent.getInstance(mContext); TVTimerManager tvManager = tv.getTimerManager(); Long mBroadcastUTC = tvManager.getBroadcastUTC(); SystemClock.setCurrentTimeMillis(mBroadcastUTC); Long mBroadcastTimeZone = tvManager.getBroadcastTZOffset(); if (SaveValue.getInstance(mContext).readValue(MenuConfigManager.SETUP_TIME_ZONE) == 25) { boolean t = SystemClock.setCurrentTimeMillis(mBroadcastUTC + mBroadcastTimeZone); } else { boolean clock = SystemClock.setCurrentTimeMillis(mBroadcastUTC); } } else if (SaveValue.getInstance(mContext).readValue(MenuConfigManager.AUTO_SYNC) == 2) { NetworkTime netTime = NetworkTime.getInstance(mContext); SystemClock.setCurrentTimeMillis(netTime.getNetTime()); } this.mDataItem = mDataItem; mTextViewName.setText(mDataItem.getmName()); if ((MenuConfigManager.TIME_TIME).equals(mDataItem.getmItemID())) { // final DateTimeView timeView = (DateTimeView) // findViewById(R.id.common_datetimeview); final UpdateTime updateProcess = new UpdateTime(MenuConfigManager.TIME_TIME); updateProcess.startprocess( new UpdateTime.UpdateListener() { public void update(String mString) { mDateTimeView.setDateStr(mString, updateProcess); mDateTimeView.postInvalidate(); } }); } else if ((MenuConfigManager.TIME_DATE).equals(mDataItem.getmItemID())) { // final DateTimeView dateView = (DateTimeView) // findViewById(R.id.common_datetimeview); final UpdateTime updateProcess = new UpdateTime(MenuConfigManager.TIME_DATE); updateProcess.startprocess( new UpdateTime.UpdateListener() { public void update(String mString) { // TODO Auto-generated method stub mDateTimeView.setDateStr(mString, updateProcess); mDateTimeView.postInvalidate(); } }); } else if ((MenuConfigManager.TIMER2).equals(mDataItem.getmItemID())) { this.mDateTimeView.setDateStr( SaveValue.getInstance(context).readStrValue(MenuConfigManager.TIMER2), new UpdateTime()); final UpdateTime updateProcess = new UpdateTime(MenuConfigManager.TIME_TIME); updateProcess.startprocess( new UpdateTime.UpdateListener() { public void update(String mString) { DateTimeInputView.this.mDateTimeView.postInvalidate(); } }); } else { this.mDateTimeView.setDateStr( SaveValue.getInstance(context).readStrValue(MenuConfigManager.TIMER1), new UpdateTime()); final UpdateTime updateProcess = new UpdateTime(MenuConfigManager.TIME_TIME); updateProcess.startprocess( new UpdateTime.UpdateListener() { public void update(String mString) { DateTimeInputView.this.mDateTimeView.postInvalidate(); } }); } this.mDateTimeView.mType = mDataItem.getmDateTimeType(); }
private void run() { // If a device's clock is before 1970 (before 0), a lot of // APIs crash dealing with negative numbers, notably // java.io.File#setLastModified, so instead we fake it and // hope that time from cell towers or NTP fixes it shortly. if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) { Slog.w(TAG, "System clock is before 1970; setting to 1970."); SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME); } // Here we go! Slog.i(TAG, "Entered the Android system server!"); EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, SystemClock.uptimeMillis()); // In case the runtime switched since last boot (such as when // the old runtime was removed in an OTA), set the system // property so that it is in sync. We can't do this in // libnativehelper's JniInvocation::Init code where we already // had to fallback to a different runtime because it is // running as root and we need to be the system user to set // the property. http://b/11463182 SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary()); // Enable the sampling profiler. if (SamplingProfilerIntegration.isEnabled()) { SamplingProfilerIntegration.start(); mProfilerSnapshotTimer = new Timer(); mProfilerSnapshotTimer.schedule( new TimerTask() { @Override public void run() { SamplingProfilerIntegration.writeSnapshot("system_server", null); } }, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL); } // Mmmmmm... more memory! VMRuntime.getRuntime().clearGrowthLimit(); // The system server has to run all of the time, so it needs to be // as efficient as possible with its memory usage. VMRuntime.getRuntime().setTargetHeapUtilization(0.8f); // Some devices rely on runtime fingerprint generation, so make sure // we've defined it before booting further. Build.ensureFingerprintProperty(); // Within the system server, it is an error to access Environment paths without // explicitly specifying a user. Environment.setUserRequired(true); // Ensure binder calls into the system always run at foreground priority. BinderInternal.disableBackgroundScheduling(true); // Prepare the main looper thread (this thread). android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND); android.os.Process.setCanSelfBackground(false); Looper.prepareMainLooper(); // Initialize native services. System.loadLibrary("android_servers"); // Check whether we failed to shut down last time we tried. // This call may not return. performPendingShutdown(); // Initialize the system context. createSystemContext(); // Create the system service manager. mSystemServiceManager = new SystemServiceManager(mSystemContext); LocalServices.addService(SystemServiceManager.class, mSystemServiceManager); // Start services. try { startBootstrapServices(); startCoreServices(); startOtherServices(); } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } // For debug builds, log event loop stalls to dropbox for analysis. if (StrictMode.conditionallyEnableDebugLogging()) { Slog.i(TAG, "Enabled StrictMode for system server main thread."); } // Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }