How to get the "last sync" time for an account?

Is it possible to get the time during which the last account was synchronized, for example, in the system settings-> Accounts & Sync? I am using Android 2.2.

Looking at source 2.2 for AccountSyncSettings.java , I see that the status is retrieved with:

 SyncStatusInfo status = ContentResolver.getSyncStatus(account, authority); 

but SyncStatusInfo and getSyncStatus do not seem to be part of the public API (marked with @hide). Is there any other way to get this information?

+6
source share
2 answers

You can use reflection to achieve this. Here is my code for implementing this

 private long getLasySyncTime() { long result = 0; try { Method getSyncStatus = ContentResolver.class.getMethod( "getSyncStatus", Account.class, String.class); if (mAccount != null && mSyncAdapter != null) { Object status = getSyncStatus.invoke(null, mAccount, mSyncAdapter.authority); Class<?> statusClass = Class .forName("android.content.SyncStatusInfo"); boolean isStatusObject = statusClass.isInstance(status); if (isStatusObject) { Field successTime = statusClass.getField("lastSuccessTime"); result = successTime.getLong(status); TLog.d(WeixinSetting.class, "get last sync time %d", result); } } } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { TLog.d(WeixinSetting.class, e.getMessage() + e.getCause().getMessage()); } catch (IllegalArgumentException e) { } catch (ClassNotFoundException e) { } catch (NoSuchFieldException e) { } catch (NullPointerException e) { } return result; } 
+3
source

The Settings app uses ContentResolver.getSyncStatus(account, authority) . However, this is not part of the public API. You can use it, but it can break with any future version.

+2
source

Source: https://habr.com/ru/post/892398/


All Articles