【问题标题】:Android syncadapter onPerformSync not called未调用 Android 同步适配器 onPerformSync
【发布时间】:2016-01-08 19:56:51
【问题描述】:

尝试为我的内容提供商设置同步适配器/服务。内容提供者工作正常,能够毫无问题地存储数据。

我有一个向我的内容解析器注册的内容观察器,并且在数据更改后调用 onChange 方法。

但是,在我调用observer.onChange 之后,日志中没有出现任何内容。在observer.onChange 中,我调用了contentResolver.requestSync,但未调用同步适配器中的onPerformSync 方法。

想法?

syncadapter.xml

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

身份验证器.xml

<account-authenticator
    android:label="@string/app_name"
    android:smallIcon="@mipmap/ic_launcher"
    android:icon="@mipmap/ic_launcher"
    android:accountType="....contentproviderexample"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

观察者 onChange:

@Override
        public void onChange(boolean selfChange, Uri uri) {
            Log.d(PERSON_OBSERVER_TAG, "onChange:  Calling requestSync");
            contentResolver.requestSync(newAccount, PersonContract.CONTENT_AUTHORITY, Bundle.EMPTY);
        }

清单:

<service android:name=".datasync.AuthenticatorService">
            <intent-filter>
                <action android:name="android.accounts.AccountAuthenticator"></action>
            </intent-filter>
            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator"/>
        </service>

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

            </meta-data>
        </service>

【问题讨论】:

  • 在 onChange 函数中放置一个调试点,首先确保正在调用。如果是,我们可以向前看。一个常见的错误

标签: android android-syncadapter


【解决方案1】:

由于 SyncService 在其自己的进程中运行,这与您的应用程序不同,由 android:process=":sync" 定义,因此不会出现日志。虽然 SyncAdapter 正在运行。

要查看日志而不是“仅显示选定的应用程序”,请在 logcat 中选择“无过滤器”。

您不必删除 android:process=":sync"。

【讨论】:

    【解决方案2】:

    通过删除以下内容,一切正常:

    android:process=":sync"
    

    从 SyncService 配置中删除了该内容,并在之后调用 onPerformSync 方法并更新到内容提供者。

    谢谢

    【讨论】:

    • 这是一个技巧,这意味着您正在调用后台服务,但是很好的解决方案或补丁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多