应用通知显示上次打开时间的毫秒级时间差问题
Hey there! Let's sort out that time difference problem you're facing when displaying the last app open date in notifications. First, let's break down the gaps in your current code and then walk through a robust solution.
Issues in the Current Implementation
Your existing code has a couple of key problems that lead to inaccurate millisecond-level time calculations:
- Incomplete time range handling: You only account for time differences larger than a day, ignoring hours, minutes, seconds, and even the first-time open scenario.
- Boundary condition error: Using
ms > DAYmeans a time difference exactly equal to one day won't be categorized correctly. - No millisecond edge case handling: Time differences smaller than a second (pure milliseconds) aren't addressed at all.
Step-by-Step Solution
Let's rewrite the code to fix these issues, with proper handling for all time intervals and millisecond precision.
1. Clean Up the Time Difference Calculation
First, simplify how you fetch and calculate the time difference to make it more readable and less error-prone:
// Get the last open time from settings (default to 0 if settings are null) long lastOpenTimestamp = MainPageActivity.SettingsInfo != null ? MainPageActivity.SettingsInfo.getLong("LASTOPEN", 0) : 0; // Calculate time difference in milliseconds long timeDiffMs = System.currentTimeMillis() - lastOpenTimestamp;
2. Refine the Time Formatting Method
Update your getTime() method to cover all possible time ranges, handle pluralization, and fix boundary conditions:
String getTime(long ms) { final int SECOND = 1000; final int MINUTE = 60 * SECOND; final int HOUR = 60 * MINUTE; final int DAY = 24 * HOUR; // Handle first-time app open or instant re-open (ms <= 0) if (ms <= 0) { return "Just now"; } String timeText; if (ms >= DAY) { long days = ms / DAY; timeText = days + (days == 1 ? " Day ago" : " Days ago"); } else if (ms >= HOUR) { long hours = ms / HOUR; timeText = hours + (hours == 1 ? " Hour ago" : " Hours ago"); } else if (ms >= MINUTE) { long minutes = ms / MINUTE; timeText = minutes + (minutes == 1 ? " Minute ago" : " Minutes ago"); } else if (ms >= SECOND) { long seconds = ms / SECOND; timeText = seconds + (seconds == 1 ? " Second ago" : " Seconds ago"); } else { // Handle time differences smaller than 1 second (milliseconds only) timeText = "Just now"; } return timeText; }
3. Ensure Accurate Timestamp Storage
Don't forget to update the LASTOPEN timestamp every time the app is opened (or resumed) to ensure future calculations are precise. Add this to your MainPageActivity's lifecycle:
@Override protected void onResume() { super.onResume(); // Update last open time whenever the app comes to the foreground if (MainPageActivity.SettingsInfo != null) { MainPageActivity.SettingsInfo.edit() .putLong("LASTOPEN", System.currentTimeMillis()) .apply(); } }
Key Improvements
- Full time range coverage: Handles everything from milliseconds to days, including the first-time open scenario.
- Boundary fixes: Uses
>=instead of>to correctly categorize exact time intervals (e.g., exactly 1 day). - User-friendly wording: Adds pluralization to make notifications sound natural (e.g., "1 Day ago" vs "2 Days ago").
- Millisecond precision: Properly handles sub-second time differences by displaying "Just now".
内容的提问来源于stack exchange,提问作者Jyoti JK

