SyncAdapter onPerformSync并不总是被调用

时间:2015-12-24 18:57:00

标签: java android android-syncadapter

我为我的应用构建了SyncAdapter,以便我可以使用Google Cloud Messaging触发数据库与服务器的同步。我正在使用Volley来实际进行网络呼叫并同步数据,但是从我想要同步你的应用时我所阅读的内容中你应该有SyncAdapter

我的问题是onPerformSync()并不总是运行。我将解雇GCM并且我总是得到一个日志,说明它已正确通过GCM,但我onPerformSync()的日志并不总是触发。因为有时我会想象它设置得当。但是,当它没有

时,我无法弄清楚发生了什么

onPerformSync()

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
    Log.d("onPerformSync", "got to the sync");
}

GCM消息处理程序中的onMessageReceived()

@Override
public void onMessageReceived(String from, Bundle data) {

    if (from.equals("/topics/global")) {
        Log.d("gcm topics", data.getString("message"));
        try {
            if (data.getString("message").equals("update")) {
                Log.d("is update", "is message update");
                Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
                ContentResolver.requestSync(newAccount, AUTHORITY, data);
            }
        } catch (NullPointerException e) {
            Log.e("GCM", e.toString());
        }

    } else {
        String message = data.getString("message");
        createNotification(from, message);
    }
}

在MainActivity中创建帐户

public static Account createSyncAccount(Context context) {
    // Create the account type and default account
    Account newAccount = new Account(
            ACCOUNT, ACCOUNT_TYPE);
    // Get an instance of the Android account manager
    AccountManager accountManager =
            (AccountManager) context.getSystemService(
                    ACCOUNT_SERVICE);
    /*
     * Add the account and account type, no password or user data
     * If successful, return the Account object, otherwise report an error.
     */
    if (accountManager.addAccountExplicitly(newAccount, null, null)) {
        /*
         * If you don't set android:syncable="true" in
         * in your <provider> element in the manifest,
         * then call context.setIsSyncable(account, AUTHORITY, 1)
         * here.
         */
        ContentResolver.setIsSyncable(newAccount, ArmyContract.CONTENT_AUTHORITY, 1);
        ContentResolver.setSyncAutomatically(newAccount, ArmyContract.CONTENT_AUTHORITY, true);

        return newAccount;
    } else {
        /*
         * The account exists or some other error occurred. Log this, report it,
         * or handle it internally.
         */
        Log.e("Account Creation", "Error withou dummy accocunt");
        return null;
    }
}

syncadapter.xml

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.clashtoolkit.clashtoolkit"
    android:accountType="clashtoolkit.com"
    android:userVisible="false"
    android:supportsUploading="false"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true"/>

authenticator.xml

<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="clashtoolkit.com"
    android:icon="@mipmap/ic_launcher"
    android:smallIcon="@mipmap/ic_launcher"
    android:label="@string/app_name"/>

AndroidManifext.xml

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.clashtoolkit.clashtoolkit.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.clashtoolkit.clashtoolkit.permission.C2D_MESSAGE" />
<service
        android:name="com.clashtoolkit.clashtoolkit.network.AuthenticatorService">
        <intent-filter>
            <action android:name="android.accounts.AccountAuthenticator"/>
        </intent-filter>
        <meta-data
            android:name="android.accounts.AccountAuthenticator"
            android:resource="@xml/authenticator" />
    </service>
    <service
        android:name="com.clashtoolkit.clashtoolkit.network.SyncService"
        android:exported="true"
        android:process=":sync">
        <intent-filter>
            <action android:name="android.content.SyncAdapter"/>
        </intent-filter>
        <meta-data android:name="android.content.SyncAdapter"
            android:resource="@xml/syncadapter" />
    </service>

1 个答案:

答案 0 :(得分:3)

问题可能在于将这些密钥添加到Bundle数据中:

// Disable sync backoff and ignore sync preferences. In other words...perform sync NOW!
        data.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        data.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        ContentResolver.requestSync(newAccount, AUTHORITY, data);