ContentProvider:如何取消先前对delete()的调用? [英] ContentProvider: How to cancel a previous call to delete()?

查看:71
本文介绍了ContentProvider:如何取消先前对delete()的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义 ContentProvider .对于查询,有一个 CancellationSignal (API 16+),可以用于取消先前对

I'm using a custom ContentProvider. For querying, there is a CancellationSignal (API 16+) which can be used to cancel a previous call to query().

我的问题:如何使用 delete()将其存档?为了澄清起见,我的自定义提供程序管理SD卡上的文件,因此我希望能够取消提供程序内部的删除操作.

My question: How can I archive that with delete()? For clarification, my custom provider manages files on SD card, and so I want to be able to cancel delete operation inside my provider.

推荐答案

我用一个简单的解决方案解决了这个问题.

I solved this with a simple solution.

例如,对于每次对 query()的调用,我们都放置一个指向任务ID的参数,并使用

For example, for every call to query(), we put a parameter pointing to the task ID, and use a SparseBooleanArray to hold that ID, like:

...
private static final SparseBooleanArray _MapInterruption = new SparseBooleanArray();

...
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Cursor cursor = ...

    int taskId = ... // obtain task ID from uri
    boolean cancel = ... // obtain cancellation flag from uri
    if (cancel) {
        _MapInterruption.put(taskId, true);
        return null; // don't return any cursor
    } else {
        doQuery(taskId);
        if (_MapInterruption.get(taskId)) {
            _MapInterruption.delete(taskId);
            return null; // because the task was cancelled
        }
    }

    ...
    return cursor;
}// query()

private void doQuery(int taskId) {
    while (!_MapInterruption.get(taskId)) {
        ... // do the task here
    }
}// doQuery()

用法:

  • 要查询:

  • To query:

...
getContentResolver().query("content://your-uri?task_id=1", ...);

  • 要取消:

  • To cancel:

    ...
    getContentResolver().query("content://your-uri?task_id=1&cancel=true", ...);
    

  • 有关完整的可行解决方案,请查看 android-filechooser .

    For a complete working solution, have a look at android-filechooser.

    优点是您可以在Android…1+及其他方法中使用此技术,例如 delete() update() ......="http://developer.android.com/reference/android/os/CancellationSignal.html" rel ="nofollow"> CancellationSignal 仅在API 16+中可用,并且仅限于 query().

    The advantage is you can use this technique in Android… 1+ and for other methods such as delete(), update()... While CancellationSignal is only available in API 16+ and is limited to only query().

    这篇关于ContentProvider:如何取消先前对delete()的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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