2011-10-07 5 views
5

मुझे उस समय की माप करने की आवश्यकता है जो कुछ घंटों तक टिक सके। मैं यह करने के लिए कुछ करना होगा सामान्य तरीके से संभालने हूँ:आप एंड्रॉइड गेम में घड़ी के शोषण से कैसे बच सकते हैं?

Date date = new Date(); 
...wait some time... 
(new Date()).getTime() - date.getTime()) 

लेकिन एक उपयोगकर्ता परिवर्तन एंड्रॉयड की घड़ी वापस एक घंटे खेल धोखा और समयावधि कम करने के लिए कर सकता है? ऑनलाइन स्रोत से समय पढ़ना सबसे अच्छा समाधान होगा?

उत्तर

9

new Date()System.currentTimeMillis() का उपयोग करता है जो ओएस घड़ी पर निर्भर करता है - और जब उपयोगकर्ता सिस्टम दिनांक और समय बदलता है तो वह बदल सकता है।

आप जो गुण निम्नलिखित है System.nanoTime() का उपयोग करना चाहिए,:

/** 
* Returns the current value of the most precise available system 
* timer, in nanoseconds. 
* 
* <p>This method can only be used to measure elapsed time and is 
* not related to any other notion of system or wall-clock time. 
* The value returned represents nanoseconds since some fixed but 
* arbitrary time (perhaps in the future, so values may be 
* negative). This method provides nanosecond precision, but not 
* necessarily nanosecond accuracy. No guarantees are made about 
* how frequently values change. Differences in successive calls 
* that span greater than approximately 292 years (2<sup>63</sup> 
* nanoseconds) will not accurately compute elapsed time due to 
* numerical overflow. 
* 
* <p> For example, to measure how long some code takes to execute: 
* <pre> 
* long startTime = System.nanoTime(); 
* // ... the code being measured ... 
* long estimatedTime = System.nanoTime() - startTime; 
* </pre> 
* 
* @return The current value of the system timer, in nanoseconds. 
* @since 1.5 
*/ 
public static native long nanoTime(); 

यह सिर्फ प्रणाली की तारीख बदलकर नहीं हेरफेर किया जा सकता।

+0

बिल्कुल सही, धन्यवाद। – FoppyOmega

+1

कोई भी आईओएस एसडीके के बराबर जानता है? – Tocco

+0

एक नकारात्मक बात यह है कि सीपीयू रीसेट होने पर नैनोटाइम रीसेट हो जाएगा। इसका मतलब है कि फोन बंद होने पर आप लंबित समय को माप नहीं सकते हैं। – sjkm

संबंधित मुद्दे