Rss feed of the tag

Enabling / Disabling activities at runtime

Written by Xavier Gouchet - 20 february 2013 - no comments

Sometimes, you might want an activity to be available only under specific conditions. But you may want to completely disable it at runtime when some conditions are met. For instance based on the time, or the user geolocation.

This could also let one have different activities based on the user's configuration

You can do so simply by using the PackageManager. In the following example, I disable an activity and enable another one.

PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(new ComponentName(this, ActivityC.class), 
pm.setComponentEnabledSetting(new ComponentName(this, ActivityA.class), 

The manifest declares the two activities with the same intent filter, namely the home screen shortcut. But using the previous code, I'm able to get a single one of them in the launcher menu at any given time.

Embedding meta data in an application's manifest

Written by Xavier Gouchet - 19 november 2012 - 1 comment

A little tip I just discovered : besides having information on Activities, Services and such, you can embed any metadata inside an application's manifest.

To do this, simply include the following meta-data tag in an application, activity, activity-alias, service or receiver. The tag can either be used to store a value (int, float, string) or a resource id :

    android:value="1.5" />

    android:resource="@string/meta_owner" />

The meta data is stored in a Bundle instance, which you can retrieve using one of the following lines :

PackageManager pm = getPackageManager();
PackageItemInfo info;

info = pm.getActivityInfo(component, PackageManager.GET_META_DATA);
info = pm.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
info = pm.getReceiverInfo(component, PackageManager.GET_META_DATA);
info = pm.getServiceInfo(component, PackageManager.GET_META_DATA);
Bundle metaData = info.metaData; 

You can then read any metadata stored in the bundle using the getter methods.