开发者

Passing Parameters via URI in Android

开发者 https://www.devze.com 2023-02-18 12:24 出处:网络
Is it possible (or recommended) to pass parameters to content providers via URIs in Android, similar to how web addresses use them?That is to say, can I use name/value pairs in content:// URIs?

Is it possible (or recommended) to pass parameters to content providers via URIs in Android, similar to how web addresses use them? That is to say, can I use name/value pairs in content:// URIs?

For example, I have a search provider that can search based on names. I pass it a URI like this:

content://com.example.app/name/john

That would return anyone with "john" in their names, including John, Johnathon, Johnson, etc.

I want to have the option (but not requirement) to search by exact names and not find partial matches. I was thinking of doing something like this:

content://com.example.app/name/john?exact=true

That would tell the search provider to only 开发者_如何学编程return names that exactly match "John." But I haven't seen any other examples of parameters used like this within Android. Is there a better way? What am I missing here?

Thanks!


if you want to pass query parameters you can use the appendQueryParameter() method when constructing your URI

LoaderFactory.createCursorLoader(this,
                MyProvider.MY_CONTENT_URI.buildUpon().appendQueryParameter(
                        MyProvider.MY_PARAM_NAME,
                        myParamValue).build(), projection, null,
                null, null);

And then access the param value using the getQueryParameter()

String paramValue = uri.getQueryParameter(MyProvider.MY_PARAM_NAME);


No, as far as I have seen, any parameters get stripped from content provider urls (although I don't know why), I worked around this by adding parameters using a "/" and a certain prefix and parse them out manually, something like this:

content://com.example.app/name/john/paramexact/true
0

精彩评论

暂无评论...
验证码 换一张
取 消