question

Monika Deshpande avatar image
Monika Deshpande asked

Phonegap - css, js and Amazon mobile ad api

If anyone has an example of using Amazon mobile ads api in a HTML/ CSS/ JS phonegap app for android or ios, it will be great if you can share it. I only found Java examples of using Amazon mobile ads api, no javascript examples. Anyone? Please? Thanks
mobile ads
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
There is no public library that would help you to integrate Amazon Ads in your web app. But following thing is allowed in Phonegap based hybrid app. public class HelloWorld extends DroidGap implements AdListener { private static final String APP_KEY = "sample-app-v1_pub-2"; // Sample Application Key. Replace this variable with your Application Key private static final String LOG_TAG = "SimpleAdSample"; // Tag used to prefix all log messages private AdLayout adView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // loadViewInLinearLayout(); loadViewInRelativeLayout(); } private void loadViewInRelativeLayout() { // Create the layout RelativeLayout layout = new RelativeLayout(this); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); super.loadUrl(Config.getStartUrl()); ((ViewGroup) appView.getParent()).removeView(appView); layout.addView(appView); AdRegistration.setAppKey(APP_KEY); AdRegistration.enableLogging(true); AdRegistration.enableTesting(true); AdLayout adView = new AdLayout(this, AdSize.SIZE_600x90); adView.loadAd(new AdTargetingOptions()); ViewGroup.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); ((RelativeLayout.LayoutParams) adParams).addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); ((RelativeLayout.LayoutParams) adParams).addRule(RelativeLayout.ALIGN_PARENT_LEFT); adView.setLayoutParams(adParams); FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(600, 90); adView.setLayoutParams(lp); adView.loadAd(new AdTargetingOptions()); layout.addView(adView); setContentView(layout); } private void loadViewInLinearLayout() { // Set by in config.xml super.loadUrl(Config.getStartUrl()); ((ViewGroup) appView.getParent()).removeView(appView); //super.loadUrl("file:///android_asset/www/index.html") LinearLayout linearLayout = new LinearLayout(this); linearLayout.setOrientation(LinearLayout.VERTICAL); adView = new AdLayout(this, AdSize.SIZE_600x90); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(600, 90); adView.setLayoutParams(lp); linearLayout.addView(adView); AdRegistration.enableLogging(true); AdRegistration.enableTesting(true); adView.setListener(this); appView.getSettings().setJavaScriptEnabled(true); try { AdRegistration.setAppKey(APP_KEY); } catch (Exception e) { Log.e(LOG_TAG, "Exception thrown: " + e.toString()); return; } linearLayout.addView(appView); setContentView(linearLayout); adView.loadAd(new AdTargetingOptions()); } public void LoadAd() { // Load the ad with the appropriate ad targeting options. AdTargetingOptions adOptions = new AdTargetingOptions(); adView.loadAd(adOptions); } /** * This event is called after a rich media ads has collapsed from an expanded state. */ @Override public void onAdCollapsed(AdLayout view) { Log.d(LOG_TAG, "Ad collapsed."); } /** * This event is called if an ad fails to load. */ @Override public void onAdFailedToLoad(AdLayout view, AdError error) { Log.w(LOG_TAG, "Ad failed to load. Code: " + error.getCode() + ", Message: " + error.getMessage()); } /** * This event is called once an ad loads successfully. */ @Override public void onAdLoaded(AdLayout view, AdProperties adProperties) { Log.d(LOG_TAG, adProperties.getAdType().toString() + " Ad loaded successfully."); } /** * This event is called after a rich media ad expands. */ @Override public void onAdExpanded(AdLayout view) { Log.d(LOG_TAG, "Ad expanded."); } } Thanks.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Monika Deshpande avatar image
Monika Deshpande answered
Thanks Sujoy, Your helpful answer really appreciated. Quick follow-up question: Once I have this working (very close now) do you know how I can refresh ad when user interacts with my js/ css phonegap application (e.g. by clicking on something in my phonegap app).
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Coolabs, Sure thing. You need to set up a javascript bridge to trigger a native method (java) from the javascript. In the native java method you will refresh the add. http://developer.android.com/guide/webapps/webview.html#BindingJavaScript public class JSInterface{ private WebView mAppView; private MainActivity mainActivity; public JSInterface(MainActivity activity, WebView view) { mAppView = view; mainActivity = activity; } public String refreshAd(){ mainActivity.loadAd(); } } In your main activity add a Javascript interface for this class: public class MainActivity extends DroidGap { private JSInterface jsInterface; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); jsInterface= new JSInterface (this, appView); appView.addJavascriptInterface(jsInterface, "jsInterface"); super.loadUrl(getString(R.string.url)); } } And at last, from Javascript call window.MyCls methods: Thanks for reaching out. Hope this helps.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.