难道SyncAdapter得到通知时的AccountManage

难道SyncAdapter得到通知时的AccountManage

本文介绍了难道SyncAdapter得到通知时的AccountManager删除帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的问题是重申当你去设置 - >帐户与同步,选择创建的,你的 SyncAdapter 正在同步与云服务器的帐户,然后选择删除帐户,就会发生什么作为你的 SyncAdapter 是什么呢?有显示要求您确认,并与该帐户相关联的手机上的数据将被删除的对话框。我不能轻易相信,这个框架可以自动删除数据我 SyncAdapter 已存储在本地数据库中,但它似乎暗示,取消帐户(和我都同意,是应该)删除数据。有没有除了我的 SyncAdapter 之类的,将作为回调的帐户删除,以处理从本地数据库中删除所有适当的数据?也许它必须通过的AccountManager ,而不是进行;我的的AccountManager 得到通知时,该帐户被删除,并从那里我可以触发数据删除,而不 SyncAdapter

So, my question restated is when you go to Settings -> Accounts & Sync and select the an account that was created that your SyncAdapter is syncing with a cloud server, and select remove account, what happens as far as your SyncAdapter is concerned? There is a dialog that displays asking you to confirm and that the data on the phone associated with that account will be removed. I cannot easily believe that the framework can automatically remove the data my SyncAdapter has stored in the local database, but it seems to imply that removing the account will (and I would agree that is should) remove that data. Is there a addition to my SyncAdapter that will serve sort of as the callback for the account removal to handle deleting all the appropriate data from the local database? Maybe it has to be done through the AccountManager instead; my AccountManager gets notified when the account gets removed and from there I can trigger the data deletion without the SyncAdapter.

编辑:与此相关的,是同步的经理打电话给我的 SyncAdapter 为每个帐户进行同步,当一个新的帐户被添加?我看到一个 onPerformSync(...)沿与刚才添加的帐户previously添加帐户时我添加帐户,并希望停止执行。

On a related note, is the sync manager calling my SyncAdapter for each account that it synchronizes when a new account is added? I see a onPerformSync(...) being executed for previously added accounts along with the just added account when I add an account, and would like to stop that.

推荐答案

我发现的解决方案是使应用程序的的ContentProvider 实施 OnAccountsUpdateListener 。装上的ContentProvider 在其的onCreate 方法监听器与 account_manager.addOnAccountsUpdatedListener(这一点,空,FALSE),然后实现像

I discovered the solution is to make the app's ContentProvider implement OnAccountsUpdateListener. Attach the ContentProvider as a listener in its onCreate method with account_manager.addOnAccountsUpdatedListener(this, null, false) and then implement the interface method like

@Override
public void onAccountsUpdated(final Account[] accounts) {
    Ln.i("Accounts updated.");
    final Iterable<String> account_list = new Iterable<String>() {
        @Override
        public Iterator<String> iterator() {
            return new Iterator<String>() {
                private final Iterator<Account> account_list = Arrays.asList(accounts).iterator();

                @Override
                public boolean hasNext() {
                    return account_list.hasNext();
                }

                /** Extracts the next account name and wraps it in single quotes. */
                @Override
                public String next() {
                    return "'" + account_list.next().name + "'";
                }

                @Override
                public void remove() { throw new UnsupportedOperationException("Not implemented"); }
            };
        }
    };
    final String account_set = TextUtils.join(", ", account_list);
    Ln.i("Current accounts: %s", account_set);

    // Removes content that is associated with accounts that are not currently connected
    final SelectionBuilder builder = new SelectionBuilder();
    builder.table(Tables.CALENDARS)
           .where(Calendars.CALENDAR_USER + " NOT IN (?)", account_set);

    new SafeAsyncTask() {
        @Override
        public Void call() throws Exception {
            _model.openWritableDatabase();
            _model.delete(builder);
            return null;
        }
    }.execute();


    getContext().getContentResolver().notifyChange(Calendars.NO_SYNC_URI, null, false);
}

我建立的当前连接帐户的字符串,然后建立与SQL查询字符串。我在该查询执行的数据库上删除后台线程删除与当前未连接帐户相关的数据。与我通知内容改变,但并不需要与服务器同步。

I construct a String of the currently connected accounts, then build a SQL query with that String. I perform a delete on the database in a background thread on that query to remove the data associated with accounts not currently connected. And I notify that content changed, but does not need to synchronized with the server.

这篇关于难道SyncAdapter得到通知时的AccountManager删除帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:31