Vote count:
0
Android has the solution to define meta-data
in the manifest. I think this is a very nice solution for people who are building SDK's. Why? Because if you have to define a certain set of keys in the String file it would be nicer to only set them in the Manifest. They are more in context with the application and its friendlier for the developer implementing your SDK in their project since they only have to add some lines in the manifest instead of editing two or more files for only an api key/sender_id
.
So to the point
I want the user to place a large number in their manifest so I can retrieve it and retrieve it from the SDK.
<meta-data
android:name="com.example.projectId"
android:value="385167817594" />
This is my code for retrieving the Meta-data. Where META_PROJECT_ID = com.example.projectId
String pkgName = mContext.getPackageName();
Bundle bundle = mContext.getPackageManager().getApplicationInfo(pkgName, PackageManager.GET_META_DATA).metaData;
String projectId = bundle.getString(META_PROJECT_ID);
This code works with all my other values defined in the manifest. But the fun part is, when retrieving values from the manifest it looks like some parsing is done inbetween. Even if I do getString(..)
it will return an int
and not a String
since the projectId only contains numbers and is not alphanumric
. If I add one letter to the projectId value defined in the manifest it will return a String. Example: 385167817594a
I think this is not a problem for the most of us. They will do getInt and then later convert it to String when necessary. But this int is too big. The Bundle isn't even showing the correct value in the debugger. Its showing a large negative number that probably indicates parsing went wrong?
How did others do it?
Facebook
encountered the same problem i guess. What they did is the following:
Manifest (for example)
<meta-data
android:name="com.example.projectId"
android:value="R.string.projectId" />
Strings.xml
<string name="projectId">385167817594</string>
Does anyone know why Facebook
decided to solve it like this? I mean now you have to edit two files and reference from the manifest to the Strings manifest.
Is there really no solution or is what Facebook did best practice
?
For now i will do it like Facebook did, but im really curious if there is not a solution and if what Facebook did is best practice
Android Meta-Data int to big for parsing and not seen as String
Aucun commentaire:
Enregistrer un commentaire