मुझे पता है कि यह एक बहुत पुराना सवाल है, लेकिन, मैं SensorEvent.timestamp
को मानव पठनीय समय में बदलने के लिए भी संघर्ष कर रहा हूं। इसलिए मैं यहां लिख रहा हूं कि अब तक मुझे क्या समझ आया है और मैं आपसे बेहतर समाधान प्राप्त करने के लिए इसे कैसे परिवर्तित कर रहा हूं। किसी भी टिप्पणी का स्वागत किया जाएगा।
जैसा कि मैंने समझा, SensorEvent.timestamp
डिवाइस के बूट-अप के बाद एक विलुप्त समय है। तो मुझे डिवाइस के अपटाइम को जानना है। तो अगर कोई एपीआई रिटर्निंग डिवाइस बूट-अप है, तो यह बहुत आसान होगा, लेकिन, मुझे यह नहीं मिला है। तो मैं डिवाइस के अपटाइम को 'अनुमान' करने के लिए SystemClock.elapsedRealtime()
और System.currentTimeMillis()
का उपयोग कर रहा हूं। यह मेरा कोड है।
private long mUptimeMillis; // member variable of the activity or service
...
atComponentsStartUp...() {
...
/* Call elapsedRealtime() and currentTimeMillis() in a row
in order to minimize the time gap */
long elapsedRealtime = SystemClock.elapsedRealtime();
long currentTimeMillis = System.currentTimeMillis();
/* Get an uptime. It assume that elapsedRealtime() and
currentTimeMillis() are called at the exact same time.
Actually they don't, but, ignore the gap
because it is not a significant value.
(On my device, it's less than 1 ms) */
mUptimeMillis = (currentTimeMillis - elapsedRealtime);
....
}
...
public void onSensorChanged(SensorEvent event) {
...
eventTimeMillis = ((event.timestamp/1000000) + mUptimeMillis);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(eventTimeMillis);
...
}
मुझे लगता है कि यह ऐप्स के लिए काम करता है कि एक मिलीसेकंद समय त्रुटि ओकी है। कृपया, अपने विचार छोड़ दें।
स्रोत
2016-02-17 14:09:39
यहाँ देखें: http://stackoverflow.com/questions/3498006/sensorevent-timestamp-to-absolute-utc-timestamp – goto10