在SyncAdapter使用ContentResolver的,而不是ContentProviderClient [英] Using ContentResolver instead of ContentProviderClient in SyncAdapter

查看:843
本文介绍了在SyncAdapter使用ContentResolver的,而不是ContentProviderClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从文档了解,在SyncService定义的一个SyncAdapter仅限于接受只有一个ContentProvider的权力去努力。

As I understand from the docs, one SyncAdapter defined in a SyncService is limited to receive only one ContentProvider authority to work on.

但是,与此同时,它有访问ContentResolver的它允许运行查询其他ContentProviders为好。如果需要开发者提供一个单一的内容授权SyncAdapter和她仍然能够做自己想要的一切对她的ContentProvider访问到任何我不明白,这个特殊的设计理念。我的问题是:什么是无视onPerformSync的参数的后果:字符串权威和ContentProviderClient提供商,用纯ContentResolver的准备

But, at the same time, it has access to ContentResolver which it allows to run queries on other ContentProviders as well. I don't understand this particular design concept if a developer is needed to provide a single content authority to SyncAdapter and nonetheless she is able to do whatever she wants on whatever ContentProvider she has access to. My question is: What are the consequences of ignoring onPerformSync's parameters: String authority and ContentProviderClient provider and going with pure ContentResolver?

我的应用程序(实际上它SyncService)的想​​法很简单:(在我的情况OwnCloud)查询日历服务器得到的不仅是事件(与com.android.calendar同步),而且 VTODOS ,它然后各种任务管理应用程序,我可以得到源$ C ​​$ c和/或ContentProviderContract之间进行分配。我也想过我自己的枢纽的ContentProvider,它具有基本的VTODO /任务结构,是相对于服务器只有一个。它应该是能同步处理2路与任务管理应用的不同的内容提供者,然后将其与服务器同步。

My application's (actually its SyncService) idea is simple: query a calendar server (OwnCloud in my case) to get not only events (synced with com.android.calendar) but also VTODOS, which are then distributed between various task management apps I can get source code and/or ContentProviderContract. I also thought of my own "Hub" ContentProvider, which has basic VTODO/Task structure, and is the only one compared to the server. It should be able to sync 2-way with different content providers of task management apps and then it syncs with the server.

我读<一个href=\"http://stackoverflow.com/questions/5084896/using-contentproviderclient-vs-contentresolver-to-access-content-provider\">using ContentProviderClient VS ContentResolver的访问内容提供商,我想我理解上的差异。我现在疑惑,为什么没有从Android SDK中那么强烈建议在一个SyncAdapter使用单一的ContentProvider,但你被允许使用ContentResolver的绕过这个限制。

I have read using ContentProviderClient vs ContentResolver to access content provider and I think I understand the difference. I'm now puzzled why there is so strong suggestion from android SDK to use a single ContentProvider in a single SyncAdapter and yet you are allowed to use ContentResolver to bypass that limitation.

我整天都搞清楚了这一点,并搜查数百SO /谷歌的资源上的物质(其中一些多次)。我也看到使用一个SyncAdapter同步多个ContentProviders有关的问题,但没有一个答案是任何接近使用ContentResolver的,而不是暗示。

I spent all day figuring this out and searched hundreds of SO/Google resources on the matter (some of them multiple times). I have also seen questions regarding using one SyncAdapter to sync multiple ContentProviders, but none of the answers were any close to suggesting using ContentResolver instead.

推荐答案

有从<$ C $的环境中使用时,是 ContentResolver的的API没有特别的限制C> SyncAdapter 。恕我直言,为什么框架传递的唯一原因 ContentProviderClient 权限 onPerformSync()是一个提示给开发商的方便和好心如何 SyncAdapter 意的工作。

There is no special limitation on ContentResolver's API when used from the context of SyncAdapter. IMHO, the only reason why the framework passes ContentProviderClient and authority to onPerformSync() is convenience and kind of a hint to developers as to how SyncAdapter intended work.

这其实是很容易在源$ C ​​$ C看到了 AbstractThreadedSyncAdapter.SyncThread - 传递给 ContentProviderClient onPerformSync()以标准方式获得:

This fact is easily seen in the source code for AbstractThreadedSyncAdapter.SyncThread - the ContentProviderClient passed to onPerformSync() is obtained in a standard fashion:

    @Override
    public void run() {
        Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

        // Trace this sync instance.  Note, conceptually this should be in
        // SyncStorageEngine.insertStartSyncEvent(), but the trace functions require unique
        // threads in order to track overlapping operations, so we'll do it here for now.
        Trace.traceBegin(Trace.TRACE_TAG_SYNC_MANAGER, mAuthority);

        SyncResult syncResult = new SyncResult();
        ContentProviderClient provider = null;
        try {
            if (isCanceled()) {
                return;
            }
            provider = mContext.getContentResolver().acquireContentProviderClient(mAuthority);
            if (provider != null) {
                AbstractThreadedSyncAdapter.this.onPerformSync(mAccount, mExtras,
                        mAuthority, provider, syncResult);
            } else {
                syncResult.databaseError = true;
            }
        } finally {
            Trace.traceEnd(Trace.TRACE_TAG_SYNC_MANAGER);

            if (provider != null) {
                provider.release();
            }
            if (!isCanceled()) {
                mSyncContext.onFinished(syncResult);
            }
            // synchronize so that the assignment will be seen by other threads
            // that also synchronize accesses to mSyncThreads
            synchronized (mSyncThreadLock) {
                mSyncThreads.remove(mThreadsKey);
            }
        }
    }

因此​​,bootom行:您可以使用 ContentResolver的 SyncAdapter 如你所愿 - 只需要调用的getContext()。getContentResolver()和访问任何出口的ContentProvider

Therefore, the bootom line: you can use ContentResolver in your SyncAdapter as you wish - just call getContext().getContentResolver() and access any exported ContentProvider.

这篇关于在SyncAdapter使用ContentResolver的,而不是ContentProviderClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆