Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Parameters via URI in Android

Tags:

android

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!

like image 798
Max Headroom Avatar asked Mar 21 '11 07:03

Max Headroom


2 Answers

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);
like image 146
joneswah Avatar answered Nov 13 '22 19:11

joneswah


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
like image 38
HefferWolf Avatar answered Nov 13 '22 21:11

HefferWolf