Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a meta-data value to type string?

Tags:

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

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

When accessing the meta data 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 interepreted (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 datatype 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.

like image 288
P.Melch Avatar asked Jan 28 '10 13:01

P.Melch


2 Answers

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"/> 
like image 114
Dave Webb Avatar answered Sep 23 '22 18:09

Dave Webb


I have also such 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 - eskape 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.

like image 26
Mikhail Avatar answered Sep 21 '22 18:09

Mikhail