簡體   English   中英

Android - 在設置同步適配器時啟用自動同步

[英]Android - Enable automatic sync when sync adapter is setup

我正在編寫一個使用同步適配器來同步數據的應用程序。

我已經閱讀了文檔,我很確定我理解它是如何工作的。 我的大部分同步適配器都是按照Udi Cohen寫的偉大指南來完成的。

但是,我確實有一個問題似乎無法解決,而且在安裝我的應用程序時會自動啟用同步。

當我的應用程序運行時,它會創建一個同步適配器和一個帳戶,執行您希望它完成的所有工作,這很棒。 但是,如果我轉到“設置”>“帳戶”>“我的應用程序”,則同步已關閉。 無論如何我可以讓它自動啟用嗎?

帳戶>'我的應用程序'的屏幕截圖

設置同步適配器時,我的代碼如下所示:

if(accountManager.addAccountExplicitly(account,null,null))
{
    // Inform the system that this account supports sync
    ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);

    // Inform the system that this account is eligible for auto sync
    ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);

    // Recommend a schedule for auto sync
    ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);

    newAccount = true;
}

通過閱讀同步適配器,我相信ContentResolver.setSyncAutomatically()是用於自動啟用同步的正確方法。

我也嘗試過設置ContentResolver.setMasterSyncAutomatically(true); 但這似乎沒有任何影響。

我在AndroidManifest中聲明了android.permission.WRITE_SYNC_SETTINGS權限,所以這不是問題所在。 我的項目中也有同步服務和xml文件。 我在下面附上了這些代碼。

有沒有其他人有類似的問題? 這可能是權限問題嗎? 如果有人有任何建議我會很樂意嘗試。 謝謝。

AndroidManifest.xml中

<service
    android:name=".syncadapter.CalendarSyncService"
    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/calendar_syncadapter"/>
</service>

syncadapter.xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
              android:accountType="com.companyname.rostering"
              android:allowParallelSyncs="true"
              android:contentAuthority="com.android.calendar"
              android:isAlwaysSyncable="true"
              android:supportsUploading="true"
              android:userVisible="true"/>

我最終發現了導致我的問題的原因。

在我的syncadapter.xml中,我的內容權限設置為android:contentAuthority="com.android.calendar"

但是,在我的代碼中,我使用ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);將同步設置為自動ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true); 我的內容權限值實際上設置為與XML中的內容真實性不同的內容。

設置同步適配器時的內容權限必須與XML中使用的內容權限相匹配。

將您的清單更改為如下所示:

     <service
        android:name=".SyncService"
        android:exported="true"
        >
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <meta-data
            android:name="android.content.SyncAdapter"
            android:resource="@xml/syncadapter" />
    </service>

在您的syncadapter.xml有以下屬性:

<?xml version="1.0" encoding="utf-8"?>

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="@string/content_authority"
    android:accountType="@string/sync_account_type"
    android:isAlwaysSyncable="true" />

我檢查了提供的教程鏈接和android:isAlwaysSyncablefalse的,所以我認為將其設置為true將解決問題

UPDATE

另一件事是,如果您在大於KITKAT的版本上安裝應用程序,那么您可能想要更改調用addPeriodicSync的方式:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // we can enable inexact timers in our periodic sync
        SyncRequest request = new SyncRequest.Builder().
                syncPeriodic(syncInterval, flexTime).
                setSyncAdapter(account, authority).
                setExtras(new Bundle()).build();
        ContentResolver.requestSync(request);
    } else {
        ContentResolver.addPeriodicSync(account,
                authority, new Bundle(), syncInterval);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM