Android - 为Admob库使用反射

时间:2011-04-12 08:01:06

标签: android reflection admob

如何在Admob库中使用反射,以便即使不包含Admob Jar也可以编译和运行代码?

1 个答案:

答案 0 :(得分:2)

try{        
    Class arcls = Class.forName("com.google.ads.AdRequest");        
    Constructor ct = arcls.getConstructor(new Class[0]);        
    Object adRequest = ct.newInstance();                    
    Class avcls = Class.forName("com.google.ads.AdView");        

    //testmode
    Method methTestMode = arcls.getMethod("setTesting", new Class[]{Boolean.TYPE}); 
    methTestMode.invoke(adRequest, new Object[]{new Boolean(true)});                 
    //end testmode

    Method methLoadAd = avcls.getMethod("loadAd", new Class[]{arcls});        
    Object adview = act.findViewById(R.id.adView);                   
    methLoadAd.invoke(adview, new Object[]{adRequest});          
}catch (Exception e){}