2012-06-18 14 views
15

सिंक्रनाइज़ करना प्रारंभ करें मेरे ऐप के लिए SyncAdapter है, और मेरे ऐप्स खाते को एंड्रॉइड खाता प्रबंधक में जोड़ने के लिए AccountManager है।एंड्रॉइड सिंक एडाप्टर स्वचालित रूप से

Bundle data = new Bundle(5); 
data.putString(_PEOPLE_ID, people_id); 
data.putString(_FIRST_NAME, first_name); 
data.putString(_LAST_NAME, last_name); 
data.putString(_PLAN, plan); 
data.putString(_BIRTHDAY, birthday); 
Account account = new Account(username, _ACCOUNT_TYPE); 
try { 
    boolean created; 
    created = _account_manager.addAccountExplicitly(account, 
            _cryptography.encrypt(_SEED, password), data); 
    response.accountCreated(created); 
    _account_manager.setAuthToken(account, _TOKEN_TYPE, session_token); 
    _model.updateActiveAccount(people_id, username, password); 
    SharedPreferences.Editor settings = _settings.edit(); 
    settings.putString(_ACCOUNT_TYPE, account.name); 
    settings.putString(_TOKEN_TYPE, session_token); 
    settings.commit(); 
    // Tells the content provider that it can sync this account 
    ContentResolver.setIsSyncable(account, AUTHORITY, 1); 
    final Bundle extras = new Bundle(1); 
    extras.putBoolean(SYNC_EXTRAS_INITIALIZE, true); 
    ContentResolver.addPeriodicSync(account, AUTHORITY, extras, 900); 
} catch (Exception e) { 
    Ln.e(e.getCause()); 
} 

मैं सेटिंग के माध्यम से सफलतापूर्वक खाता प्रबंधक को खाता जोड़ सकते हैं, लेकिन मैं मैन्युअल रूप से भी सेटिंग में खाता के लिए समन्वयन सक्षम करने के लिए है, यहां तक ​​कि: की तरह जब मैं खाता प्रबंधक से खाता जोड़ने के लिए मेरे कोड दिखता है हालांकि पृष्ठभूमि डेटा और सिम स्वचालित रूप से एमुलेटर पर सेटिंग्स सक्षम हैं। अगर मैं मैन्युअल रूप से सिंकिंग सक्षम करता हूं, तो सिंक ठीक प्रदर्शन किया जाता है, यह डिफ़ॉल्ट रूप से शुरू नहीं होता है।

ContentResolver.setIsSyncable(account, AUTHORITY, 1); 
ContentResolver.setSyncAutomatically(account, AUTHORITY, true); 

मैं ऊपर 2 लाइनों का उपयोग करें और यह ठीक से काम कर रहा है:

उत्तर

28
ContentResolver.setIsSyncable(account, AUTHORITY, 1); 
ContentResolver.setSyncAutomatically(account, AUTHORITY, true); 

Blehi के रूप में, कहा दिए गए विवरण का स्वचालित सिंकिंग आरंभ हो जाएगा वैश्विक सेटिंग्स, "पृष्ठभूमि डेटा" और "स्वत: समन्वयित" सक्षम हैं, दिए गए।

बैक-टू-बैक सिंक कर रहा है (from jcwenger) को रोकने के लिए यह सुनिश्चित करें कि यदि SyncAdapter.onPerformSync(...) कॉल ContentResolver.notifyChange(...) में किसी भी विधि ध्वज लगाने ContentResolver.notifyChange(uri, observer, false) का उपयोग करता एक सिंक कॉल ट्रिगर नहीं सूचित कर (तीसरी पैरामीटर syncToNetwork है)।

आप आवेषण प्रदर्शन करने के लिए ContentProvider उपयोग कर रहे हैं/हटाता है/आप SyncAdapter के लिए अद्यतन यह समझ में आता है ContentResolver.notifyChange(...) तो कॉल करने के लिए है कि जब तक एप्लिकेशन दिखाई दे रहा है उपयोगकर्ता SyncAdapter से अपडेट प्राप्त कर सकते हैं, जिसका अर्थ है आप होगा ContentProviderContentResolver.notifyChange(...) कॉल कर रहा है। इस सेट को काम पर लाने के लिए, मैंने SyncAdapter के लिए उपयोग किए जाने वाले प्रत्येक यूआरआई को) CALLER_IS_SYNC_ADAPTER क्वेरी पैरामीटर जोड़ा है। ContentProvider के लिए इस विधि जोड़े भेजे यूआरआई

/** 
* Determines if the given URI specifies that the request is coming from the sync adapter. 
* @param uri the given URI 
* @return true if the uri specifies that the request is coming from the sync adapter 
*/ 
private boolean callerIsSyncAdapter(Uri uri) { 
    final String is_sync_adapter = uri.getQueryParameter(CALLER_IS_SYNC_ADAPTER); 
    return is_sync_adapter != null && !is_sync_adapter.equals("0"); 
} 

परीक्षण करने के लिए तो आप

getContext().getContentResolver().notifyChange(uri, observer, !callerIsSyncAdapter(uri)); 

कर सकते हैं जब भी आप एक परिवर्तन सूचना भेज जरूरत है।

और यदि आप एक आवृत्ति (सर्वर को मतदान) पर समय-समय पर निष्पादित करने के लिए सिंक शेड्यूल करना चाहते हैं तो इसे ContentResolver.setSyncAutomatically(...) कॉल के साथ जोड़ें।

ContentResolver.addPeriodicSync(account, AUTHORITY, new Bundle(), frequency_in_seconds) 
6

आप डिफ़ॉल्ट रूप से syncable होने के लिए खाता सेट करना होगा।

+0

ContentResolver.setSync स्वचालित रूप से (खाता, प्राधिकरण, सत्य) जोड़ना; मुझे जो चाहिए वह है, लेकिन ऐसा लगता है कि यह आवधिक सिंक को स्वीकार नहीं कर रहा है। एक खाता जोड़ने के बाद यह बार-बार समन्वयित रहता है। –

+0

कृपया सामग्री के रूप में new Bundle() के साथ ContentResolver.addPeriodicSync() फ़ंक्शन को कॉल करने का प्रयास करें। – Blehi

+0

अतिरिक्त एक नया बंडल() है, मैं किसी भी झंडे को सेट करने के बिना भी कोशिश कर रहा हूं (बंडल एक्स्ट्रा = नया बंडल(); addPeriodicSync (खाता, प्राधिकरण, अतिरिक्त, 900);) –

13

बस पर जोर देना, ऐसा लगता है addPeriodicSync() जरूरतों setSyncAutomatically(), भले ही प्रलेखन का कहना है कि कि setSyncAutomatically() नेटवर्क का पता लगा गुदगुदी के लिए ही है।

ध्यान दें कि अवधि 60 मिनट तक ठीक हो जाएगी यदि यह एक मिनट से भी कम है।

+2

Google के दस्तावेज़ इस बिंदु पर खराब हैं – cja

+1

इनमें से कोई भी उनके दस्तावेज़ में कैसे नहीं आ रहा है? – toobsco42

2

प्रारंभ में खाता बनाएं और खाता प्रबंधक का उपयोग करके सिंक समर्थन की जांच करें। यदि खाता सिंक का समर्थन करता है तो setIsSyncable() और setSyncAutomatically() पर कॉल करें।

AccountManager accountManager = (AccountManager) this.getSystemService(ACCOUNT_SERVICE); 
if(accountManager.addAccountExplicitly(newAccount, null, null)){ 
     ContentResolver.setIsSyncable(account, AUTHORITY, 1); 
     ContentResolver.setSyncAutomatically(account, AUTHORITY, true); 
} 

फिर आप किसी भी समय सिंक ट्रिगर कर सकते हैं।यह ट्रिगर होगा।

नोट: setSyncAutomatically() परPerformSync() को पहले कहा जाएगा, तो केवल अन्य सिंक अनुरोध ट्रिगर किया जाएगा। लेकिन बल सिंक बनाने के लिए बस 2 अतिरिक्त झंडे जोड़ें।

Bundle bundle= new Bundle(); 
    bundle.putBoolean(
      ContentResolver.SYNC_EXTRAS_MANUAL, true); 
    bundle.putBoolean(
      ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 
    ContentResolver.requestSync(account, AUTHORITY, bundle); 
संबंधित मुद्दे