android - 如何通过 ContentResolver 调用 ContentProvider 中的自定义方法,然后访问 Bundle?

标签 android android-contentprovider android-contentresolver

我的自定义 ContentProvider 类 MyContentProvider 中有一个自定义方法 save(),我想通过 ContentResolver 调用它。目标是将 POJO 作为 Bundle 传递给 MyContentProvider

我正在使用提到的 call 方法 here并定义了 here .

我没有收到任何错误。该方法只是未访问。

具有自定义方法的(缩短的)自定义 ContentProvider 如下所示:

public class MyContentProvider extends ContentProvider {

    public void save() {

        Log.d("Test method", "called");
    }
}

我这样调用它:

ContentResolver contentResolver = context.getContentResolver();
Bundle bundle = new Bundle();
bundle.putSerializable("pojo", getPojo());
contentResolver.call(Contracts.CONTENT_URI, "save", null, bundle);

为什么 save 方法从未被调用,如果我到了这一点,我该如何访问 save() 方法中调用的 Uri 和 Bundle?我在 SO 或网络上的任何地方都找不到这方面的任何引用。

感谢您的回答!

最佳答案

我一直在玩这个来让自定义函数正常工作。如对您的问题的评论中所述,关键是在内容提供程序中实现 call() 方法以处理您可能传入的各种方法。

我对 ContentResolver 的调用如下所示:

ContentResolver cr = getContentResolver();

cr.call(DBProvider.CONTENT_URI, "myfunction", null, null);

在 ContentProvider 中,我实现了调用函数,它检查传入的方法名称:

@Override
public Bundle call(String method, String arg, Bundle extras) {
    if(method.equals("myfunction")) {
        // Do whatever it is you need to do
    }
    return null;
}

这似乎可行。

关于android - 如何通过 ContentResolver 调用 ContentProvider 中的自定义方法,然后访问 Bundle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16733232/

相关文章:

java - Telephony.Sms.Inbox.PERSON 使用已弃用的 Contacts.People._ID

android - 无法访问 android.support.v4.app.BaseFragmentActivityHoneycomb

android - SEAndroid进程域是如何给定的

javascript - 使用 setTimeout 或动画 css3 时如何使用 Phonegap 和固定的 div?

android - ArrayAdapter的getView与图片下载不一致

android - 从 Picasa//Google + 同步文件夹从图库中获取图片不起作用

android - 内容提供商查询()未被调用(安卓电视)

android - 使用 Async Task 读取电话联系人并将它们设置为 AutoCompleteTextView 的适配器

android - 从我的 contentProvider 获取最后一行

Android:ContentResolver.requestSync()没有触发onPerformSync()