【问题标题】:ContentProvider: How to cancel a previous call to delete()?ContentProvider:如何取消先前对 delete() 的调用?
【发布时间】:2012-12-06 09:44:10
【问题描述】:

我正在使用自定义ContentProvider。对于查询,有一个 CancellationSignal (API 16+) 可用于取消之前对 query() 的调用。

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

【问题讨论】:

    标签: android-contentprovider cancellation


    【解决方案1】:

    我用一个简单的方法解决了这个问题。

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

    ...
    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()
    

    用法:

    • 查询:

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

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

    如需完整的工作解决方案,请查看android-filechooser

    优点是您可以在 Android... 1+ 和其他方法(例如 delete()update()... @。

    【讨论】:

      猜你喜欢
      • 2019-08-28
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 2013-09-27
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多