I would like to add a custom attribute to the application tag of my AndroidManifest.xml file. Is this possible in the Android environment?
-
i guess not ...but can you elaborate this...– Its not blankApr 25, 2012 at 8:22
-
Sure, my motivations are here: stackoverflow.com/q/10311504/183123. I would like to have an application register with a service I am developing. The target application need not be running– MM.Apr 25, 2012 at 8:24
5 Answers
Yes. Here's an example. The custom tag is ContentVersion
.
<application android:name=".MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name">
<meta-data android:name="ContentVersion" android:value="1.9" />
<activity android:name="com.someone.something.MainActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="sensor"
android:label="@string/app_name">
To access it:
ApplicationInfo ai = _context.getPackageManager().getApplicationInfo(_context.getPackageName(),PackageManager.GET_META_DATA);
ai.metaData.get("ContentVersion")
-
8Huh. A downvote with no comment. That doesn't help! Please tell me what is wrong with my answer, if you ever come back!– SimonNov 1, 2012 at 18:07
-
-
It appears that you can't add <meta-data> to an <application> tag. Here's the relevant doc: developer.android.com/guide/topics/manifest/… Oct 9, 2013 at 23:28
-
@JoeBowers Thanks for that. Interesting! However, it does work, and continues to work in published apps so presumably under all later versions. I will test again though.– SimonOct 10, 2013 at 12:54
-
3OK, 2 downvotes now on a perfectly good answer. I'm not in this for the points, and I really don't care about losing 2 of them, I've got thousands, so go ahead and downvote if you wish but PLEASE have the goodness to tell me why.– SimonMar 19, 2014 at 16:03
You cannot define custom attribute to a predefined tag, but you can add key-value pairs called meta-data.
You could go for a SharedPreferences, instead (aka, settings).
-
1
-
Not completely. I was going for other solutions that could fit in this situation. Apr 25, 2012 at 8:24
-
1tags in the manifest are added at compile time and accessed at runtime. sharedpreferences are created and accessed at runtime.– njzk2Apr 25, 2012 at 8:26
-
@njzk2 manifest is accessed even before runtime of your app to e.g. install the app.– zaplApr 25, 2012 at 8:27
-
1@zapl : by the system, yes. but you can access it during the runtime of your app– njzk2Apr 25, 2012 at 8:29
In the tag (as well as service and receiver), you can use the tag (http://developer.android.com/guide/topics/manifest/meta-data-element.html )
It contains a name and a value or a resource ID.
You retrieve it through the PackageManager.
If anybody needs that for Xamarin (Mono for Android) I couldn't find the constant, but i found the value for it, which is 128.
I used a "for" condition to go through all values from 0 to 1000 and check whenever the MetaData property was not null. lol