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

时间:2013-05-24 10:47:42

标签: android android-contentprovider android-contentresolver

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

我使用here提到的call方法,并定义了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或网络上找到任何参考资料。

感谢您的回答!

2 个答案:

答案 0 :(得分:19)

我刚刚玩这个以获得自定义功能。正如您对问题的评论所述,关键是在内容提供程序中实现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;
}

这似乎有效。

答案 1 :(得分:-3)

如果要重新定义自己的ContentProvider,则必须覆盖以下方法:

  • 的onCreate()
  • 查询()
  • 删除()
  • 刀片()
  • 更新()
  • 的getType()

但是save()方法不在ContentProvider的生命周期内,也无法调用。