25

I specified a meta-data in my manifest.xml like this:

<meta-data android:value="5555555555" android:name="foo"></meta-data>

When accessing the metadata like this:

ActivityInfo ai = act.getPackageManager().getActivityInfo(componentName, PackageManager.GET_META_DATA);
Object value = (Object)ai.metaData.get(key);

this value gets interpreted as int and - more importantly - incorrectly interpreted (1260588259).

Since the type is determined automatically by the build system ( booleans, ints, floats, and strings ) I wondered if there is any way to force the data type to string.

I tried adding a space at the end ( "5555555555 "), but then the value gets interpreted as 5.5555553E9 float! ).

I also tried using getString instead of get, but then null is returned.

Any ideas? TIA.

4 Answers 4

38

Putting an escaped space in front of the number seems to work:

<meta-data android:name="foo" android:value="\ 1234567890"/>

The escaped space doesn't appear in the value returned by get().

I have no idea quite how this works, if I'm honest.

If you want something documented and therefore more reliable you can always put your string value as a resource and refer to it:

<meta-data android:name="foo" android:resource="@string/mynumber"/>
Sign up to request clarification or add additional context in comments.

Comments

9

I have also such a problem. My solution is as follows:

<meta-data android:value="5555555555\0" android:name="foo"></meta-data>

I added at the end of a variable - escape sequence "\0" (NULL - is often used to define the end of a character string (for example, in the language C).

This work for me.

Best regards.

1 Comment

I used this for a couple years but recently "5555555555\0" gets converted to "55555555550" As if the backslash was escaping the 0. So this might not be as reliable as using strings.xml file to store the number.
0

Instead of a space, try using a piece of punctuation that should have no role in a numeric value, like a colon, and see if that helps.

1 Comment

problem is that i really need to have the number unaltered. if possible. i could always use something like "num:5555555555" but i would only do it that way if there really is no other way.
0

I recently came across this problem and it is a very frustrating that something like a Facebook application id is interpreted as an integer leading to problems due to overflow (of course). I would have thought/hoped it would be possible to override the implicit typing. For example, instead of:

<meta-data android:name="value" android:value="12345678901234567890" />

it would be nice if we could write:

<meta-data android:name="value" android:type="string" android:value="12345678901234567890" />

In order to compensate for this, I wrote some supporting code that requires manifest attributes to look as follows:

<meta-data android:name="value1" android:value="string/12345678901234567890" />
<meta-data android:name="value2" android:value="integer/12345" />
<meta-data android:name="value3" android:value="boolean/true" />
<meta-data android:name="value4" android:value="float/1.34" />
<meta-data android:name="value5" android:value="float/6" />

Note that this is intended to look similar to the Android @string/@integer syntax except that I have dropped the @, interpreting this as dropping the level of indirection.

As well as solving the original problem of [0-9]+ always interpreted as integers it seems, it also allows for floats to be specified directly despite looking like an integer.

A further advantage of this is that it is possible to extend the set of types arbitrarily. e.g.

<meta-data android:name="myfrac" android:value="fraction/3/7" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.