Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the 'last sync' time for an account?

Is it possible to retrieve the time an account was last synchronized, like the system Settings->Accounts&Sync app does? I'm using Android 2.2.

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

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

but SyncStatusInfo and getSyncStatus don't seem to be part of the public API (marked with @hide). Is there some other way to get at this info?

like image 963
Simon Avatar asked Jul 09 '11 15:07

Simon


2 Answers

You can use reflection to achieve this purpose.Here is my code to implement 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;
}
like image 119
user2593910 Avatar answered Sep 30 '22 12:09

user2593910


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

like image 34
Frank Avatar answered Sep 30 '22 14:09

Frank