单击列表视图项时如何显示admob插页式广告?

时间:2019-03-27 00:39:04

标签: android android-fragments admob

我有一个包含listview的片段,我想在用户单击listview项目时显示间隙广告,我尝试了以下代码,但未显示:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

......

mInterstitialAd = new InterstitialAd(mContext);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    mInterstitialAd.loadAd(new AdRequest.Builder().build());
                }

            }
        });

......        

return rootView;
    }

2 个答案:

答案 0 :(得分:0)

设置新类,该类设置,加载和显示interstatialAd静态方法,如下所示:

public class AdSetup {
    // Application context returned by function --> Application.get()

    public static String TesttingDeviceID = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static InterstitialAd mInterstitialAd = new InterstitialAd(Application.get());  

    public static void setInterstitial() {
        try {
            if (mInterstitialAd.getAdUnitId() == null) {
                mInterstitialAd.setAdUnitId(Application.get().getString(R.string.interstitialAd));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadInterstatial() {
        try {
            setInterstitial();

            AdRequest adRequestInter = new AdRequest.Builder().build();

            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    Log.e("mInterstetial onloaded", "onloaded isLoaded" + mInterstitialAd.isLoaded());
                }

                @Override
                public void onAdFailedToLoad(int i) {
                    Log.e("mInterstetial onloaded", "onloaded onAdFailedToLoad");
                }

                @Override
                public void onAdOpened() {
//                    super.onAdOpened();
                }

                @Override
                public void onAdClosed() {
//                    super.onAdClosed();
                }

                @Override
                public void onAdClicked() {
//                    super.onAdClicked();
                }

            });
            mInterstitialAd.loadAd(adRequestInter);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void showInterstatial() {
        try {
            Log.e("mInterstetial show ", "isLoaded" + mInterstitialAd.isLoaded());
            if (mInterstitialAd.isLoaded() && mInterstitialAd.getAdUnitId() != null) {
                mInterstitialAd.show();
            } else {

                loadInterstatial();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

如何在单击列表项或新活动的onCreate()时调用此方法以符合跨国家广告政策:

  AdSetup.showInterstatial();  // Show ad if already loaded else load Ad
  AdSetup.loadInterstatial(); // Load new ad   

请确保遵循以下链接中列出的政策:

Disallowed interstitial implementations

  

应用加载或退出请不要在应用加载时以及何时放置非页内广告   作为插页式广告退出的应用程序应仅放置在页面之间   应用内容。广告不得放置在   在设备的后台或应用外部运行   环境。用户应清楚知道广告是哪个应用程序   与之关联或实现。

Recommended interstitial implementations

此外,添加应用程序类:

public class Application extends android.app.Application {
    //    public static DeviceName.DeviceInfo deviceInfo;
    private static Application _instance;


    @Override
    public void onCreate() {
        super.onCreate();
            _instance = this;

    }

//return the App context
    public static Application get() {
        return _instance;
    }
}

也请在您的Application中声明AndroidManifest.xml类,如第一行所示:

 <application
            android:name=".Application"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:allowBackup="false"
            android:supportsRtl="true"
            android:networkSecurityConfig="@xml/network_security_config"
            android:theme="@style/AppTheme">

答案 1 :(得分:0)

我刚刚在代码中实现了这一点。有用。在您提供的示例中,我没有看到是否在AdRequest.Builder之后实现了AdListener。如果没有,请参见以下示例:

ExampleInterstitial.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            ExampleInterstitial.loadAd(new AdRequest.Builder().build());

        }

    });

接下来,在您的示例中,您似乎只看到了展示onItemClick的插页式广告,但是您是否打算移到也在此处设置的新活动?我没看到在下面查看我的代码。我设置了开始新活动的意图,然后显示了添加后的内容。只是一个想法,因为这对我有用。祝你好运!

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            Intent intent;
            intent = new Intent(getActivity(), ExampleActivity.class);
            startActivity(intent);


                if (ExampleInterstitial.isLoaded()) {
                    ExampleInterstitial.show();
                }

                else {
                    Log.d("TAG", "The interstitial wasn't loaded yet.");
                }
            }

    });