2016-07-01 2 views
2

ソリューション: 私のSyncAdapterBinderをonBindメソッドで返すのを忘れました。Android SyncAdapterはインスタンス化されますが、onPerformSyncは決して呼び出されません

ご協力いただきありがとうございます。

私はこの問題を2日間解決しようとしており、基本的にすべての関連トピックを読んでいますが、これまで解決策を見つけることはできませんでした。 私はAndroid SyncAdapterを作成しました。私が書いたものはすべて正しいと思います。問題は、メソッドのonPerformSyncのコードが呼び出されないことです。私の電話のアカウント設定では、SyncAdapterが表示され、それを起動できますが、何も起こりません。それは、それがしばらくの間同期していると私に伝え、その後停止します。私が働いてAccountAuthenticatorを持っていると私は私のアカウントで自分の権威を接続し実行します。

DatabaseSyncAdapter.java

public class DatabaseSyncAdapter extends AbstractThreadedSyncAdapter { 
private final AccountManager am; 
private static final String TAG = "DatabaseSyncAdapter"; 

public DatabaseSyncAdapter(Context context, boolean autoInitialize) { 
super(context, autoInitialize); 
am = AccountManager.get(context); 
Log.d(TAG, "DatabaseSyncAdapter() called with: " + "context = [" + context + "], autoInitialize = [" + autoInitialize + "]"); 

} 

public DatabaseSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSync) { 
    super(context, autoInitialize, allowParallelSync); 
    am = AccountManager.get(context); 

} 

@Override 
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { 
    Log.d(TAG, "onPerformSync() called with: " + "account = [" + account + "], extras = [" + extras + "], authority = [" + authority + "], provider = [" + provider + "], syncResult = [" + syncResult + "]"); 
    //doing sync stuff 
} 

syncadapter.xml

:ここ

ContentResolver.setIsSyncable(account, "de.mydomain.myapp.provider",1); ContentResolver.setSyncAutomatically(account,"de.mydomain.myapp.provider",true); 

は私のCodesnippetsの一部です

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

SyncService.java

マニフェストでの
public class SyncService extends Service{ 


    private static final Object sSyncAdapterLock = new Object(); 

    private static DatabaseSyncAdapter sSyncAdapter = null; 


    @Override 

    public void onCreate() { 

    synchronized (sSyncAdapterLock) { 

    if (sSyncAdapter == null) 

    sSyncAdapter = new DatabaseSyncAdapter(getApplicationContext(), true); 

    } 

    } 

    @Override 

    public IBinder onBind(Intent intent) { 
     return null; 
    } 

} 

エントリ:

<service 
    android:name="de.mydomain.myapp.util.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> 

<provider 
    android:name="de.mydomain.myapp.util.StubProvider" 
    android:label="SyncLabel" 
    android:authorities="de.mydomain.myapp.provider" 
    android:exported="false" 
    android:syncable="true" /> 

私もStubProviderクラスを持っているが、それはグーグルからわずかStubProviderの例です。これはすべて標準コードですが、ContentResolver.requestSync(...)を呼び出すと何も起こりません。私もエラーは出ないし、すべてのログを見るためにLogcatフィルターを変更しなければならないことを知っている。 DatabaseSyncAdapterがインスタンス化されると、ログが表示されます。私は自分の権限と私のアカウントの種類が常に同じであり、requestSyncが適切な引数で呼び出されるように定数を使いました。それは私のために非常によく似た問題だった/

+0

バインダー 'public IBinder onBind(意図の意図)を返すのを忘れた{ return sSyncAdapter.getSyncAdapterBinder(); } 'Android OSはあなたの' SyncService'をバインドしようとしていますが、ヌルを取得するので 'onPerformSync'を呼び出すことはできません – Selvin

+0

それが問題でした。どうもありがとうございます!私はその間違いを見ないための馬鹿のように感じる:/あなたは私の週末を保存する! – Tmesus

答えて

0

はあなたの助けのためにどうもありがとうございます、私は私の問題は、可能性だけではわかりません。この問題を抱えている他の人:

if(intent.getAction().equals(Intent.ACTION_SYNC)) { //Do not do this!!!! 
    // Return the communication channel to the service. 
    return sSyncAdapter.getSyncAdapterBinder(); 
} else { 
    return null; 
} 

配信された意図には、私がチェックしたアクションがありませんでした。代わりにandroid.content.SyncAdapterでした。 if節を削除すると問題が解決しました。

関連する問題