如何在Admob库中使用反射,以便即使不包含Admob Jar也可以编译和运行代码?
答案 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){}