This is my new pet project that I started to develop month ago. First version include nearly 50 images and viewer application. I described here http://mvnblogbuild.blogspot.com/2013/04/android-bitmap-and-outofmemoryerror.html how I fought with android memory manager. Here I tell you about ads management in this application. On my regular work I use spring framework. That's why I decided to create the same mechanism for ad rotation in app.
For each ad network was created separate class. Settings was stored in class annotations. For example annotation for class is looks like this:
Sample ad class with AdEntityMeta:import java.lang.annotation.*;@Target(value=ElementType.TYPE)@Retention(value= RetentionPolicy.RUNTIME)public @interface AdEntityMeta {int runFrequency() default 0;String name();}
As you see we set the name of class to SampleAdNetworkName and leave runFrequency property to default 0 value. In manager class I use reflection for processing ad network classes. It iterate by classes and check annotation existence by calling:@AdEntityMeta(name = "SampleAdNetworkName")public class SampleAdNetwork {...}
If annotation exists then we can get parameters from it:Class adCl = adObj.getClass();if(adCl.isAnnotationPresent(AdEntityMeta.class)) {...}
AdEntityMeta adEntityMeta = adCl.getAnnotation(AdEntityMeta.class);Initialization method marked by AdEntityInitMeta annotation. To process it I use this code:
Here program enumerates similarly all methods from class and checks method marked by AdEntityInitMeta annotation. All input method parameters are stored in methodArgs. In this code was allowed only parameter with type Activity or inherited from it.Method[] method = adCl.getMethods();for(Method md: method) {if(md.isAnnotationPresent(AdEntityInitMeta.class)) {AdEntityInitMeta adEntityInitMeta = md.getAnnotation(AdEntityInitMeta.class);if (adEntityInitMeta.initType() == AdEntityLifecycleType.APPLICATION_INIT) {Class[] methodParamTypes = md.getParameterTypes();Object[] methodArgs = new Object[methodParamTypes.length];for (int i=0; iif (methodParamTypes[i] == Activity.class) {methodArgs[i] = this.baseActivity;}}try {md.invoke(adObj, methodArgs);} catch (Exception e) {e.printStackTrace();}}}}
If you are interested - here is my app: https://play.google.com/store/apps/details?id=com.blogspot.mvnblogbuild.wallpapergirls.corsets