Can I display google adwords (AdView) in javafx on android

…衆ロ難τιáo~ 提交于 2019-12-11 03:09:26

问题


I have a javafx app running on android and would like to place google adwords on some of the screens. The issue I have is that the adwords use googles AdView to display the add which is an Android presentation object and is not compatible with the JavaFX presentation layer.

Sample code that creates AdView

import com.google.android.gms.ads.MobileAds;

import javafxports.android.FXActivity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

/**
 * Constructor creates AdView
 * Call getAdView() to retrieve AdView object.
 */
public class GoogleAds {

    private AdView adView = null;
    public GoogleAds() {
        AdView mAdView = createAddView();
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
                .build();
        mAdView.loadAd(adRequest);
    }
...

Now I have a com.google.android.gms.ads.AdView object ready for presentation

I can add it to the android view hierarchy using

private void includeGoogleAdView(LinearLayout layout) {
    AdView adView = GoogleAds().getAdView();
    layout.addView(mAdView);
}

All this works as expected when run as an android app.

So the question is "can I get an AdView to show in the javafx presentation layer?".

Is there was a way to make an com.google.android.gms.ads.AdView become an instance of javafx.scene.Node then I could add it using something like

private Node addToPane(AdView adView) {
    javafx.scene.layout.Pane pane = new javafx.scene.layout.Pane();
    pane.getChildren().addAll( canvas, adView);
    return pane;
}

Or is there another solution?


回答1:


You can easily include an AdView in your app by getting access to the ViewGroup from the FXActivity.

This is a code snippet that works for me. Notice it has to run in the UIThread:

public AndroidAds()  {

    FXActivity.getInstance().runOnUiThread(() -> {
        LinearLayout layout = new LinearLayout(FXActivity.getInstance());
        layout.setVerticalGravity(Gravity.BOTTOM);
        layout.setOrientation(LinearLayout.VERTICAL);

        AdView adView = new AdView(FXActivity.getInstance());
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXXXXXX");

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("XXXXXXXXXXXXXXXXXXXX")
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                System.out.println("AD loaded");
            }
        });

        layout.addView(adView);

        FXActivity.getViewGroup().addView(layout);
    });

}



来源:https://stackoverflow.com/questions/39052579/can-i-display-google-adwords-adview-in-javafx-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!