我的自定义 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/