2012-10-30 22 views
11

Seguo questo titolo Why does ContentResolver.requestSync not trigger a sync? ma il metodo di sincronizzazione non viene visualizzato nella schermata Account & Sync in Setting. Penso che onPerformSync() non venga chiamato. Cerco di eseguire il debug ma non è stato trovato alcun registro degli errori. Ecco il mio codice:Impossibile eseguire la sincronizzazione (onPerformSync non viene chiamato)

syncAdapter.xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" 
android:accountType="@string/ACCOUNT_TYPE" 
android:contentAuthority="@string/AUTHORITY" 
android:userVisible="true" 
android:isAlwaysSyncable="true" 

/>

SyncService.java

public class CalendarSyncService extends Service{ 
private static SyncAdapterImpl sSyncAdapter = null; 
private static String list_event_ids; 
private static Requestor requestor; 

public void onCreate(){ 
    super.onCreate(); 
    if (sSyncAdapter == null) 
     sSyncAdapter = new SyncAdapterImpl(this); 
} 

@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return sSyncAdapter.getSyncAdapterBinder(); 
} 

public class SyncAdapterImpl extends AbstractThreadedSyncAdapter { 

    Context context; 
    public SyncAdapterImpl(Context context) { 
     super(context, true); 
     // TODO Auto-generated constructor stub 
     this.context = context; 
    } 

    @Override 
    public void onPerformSync(Account arg0, Bundle arg1, String arg2, 
      ContentProviderClient arg3, SyncResult arg4) { 
     // TODO Auto-generated method stub 
     Log.e("thuutai", "Perform Sync Call"); 

     try { 
      CalendarSyncService.performSync(context); 
     } catch (DOMException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ParserException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

Io lo chiamo qui

public void onClick(View arg0) { 
Bundle params = new Bundle(); 
     params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false); 
     params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false); 
     params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); 
     ContentResolver.addPeriodicSync(selectedAccount, getString(R.string.AUTHORITY), params, 3600); 
     ContentResolver.setSyncAutomatically(selectedAccount, getString(R.string.AUTHORITY), true); 

     ContentResolver.requestSync(selectedAccount, getString(R.string.AUTHORITY), params);} 

Manifest.xml

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

ho testato in Nexus S Android 4.0. Grazie per la tua lettura!

+0

forse questo è lo stesso problema: http://stackoverflow.com/questions/15810659/android-syncadapter-retry/16294867 – Kenumir

+0

In questa riga necessità per dare il nome del pacchetto o questa è la dichiarazione comune per tutti i progetti ?? – Madhu

+0

problema è che stai dicendo di non aggiornare quando si imposta sync_extras_manual su false dovrebbe essere vero – schwiz

risposta

1

Qualcosa che si può provare è:

ContentResolver.setSyncAutomatically(account, AUTHORITY, true); 

ho provato a fissare la bandiera isAlwaysSyncable ma non ha funzionato per me (anche se questo potrebbe essere stato un problema diverso).

7

A volte questo metodo non verrà chiamato a causa di una sincronizzazione simile in corso. È possibile controllare e annullare con il seguente codice:

if (ContentResolver.isSyncPending(yourAccount, yourAuthority) || 
    ContentResolver.isSyncActive(yourAccount, yourAuthority)) { 
    Log.i("ContentResolver", "SyncPending, canceling"); 
    ContentResolver.cancelSync(yourAccount, yourAuthority); 
} 
+0

quel problema l'ho risolto così a lungo. Comunque, grazie per la tua preoccupazione .. –

+0

Tutti hanno bisogno di usare questo! –

-1

È necessario forzare la sincronizzazione manuale.

Bundle params = new Bundle(); 
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); 
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); 
ContentResolver.requestSync(selectedAccount, getString(R.string.AUTHORITY), params); 
Problemi correlati