如何在自定义对话框中添加Facebook非页内广告?

时间:2019-05-18 08:50:05

标签: java ads facebook-android-sdk customdialog

在我的背部主要活动中的应用中,我想显示一个带有脸书插页式广告的对话框。

我创建了一个自定义对话框类,以向用户显示对话框,以显示用户是否要退出,而在对话框中向用户显示广告时,请点击监听器。

对于此自定义对话框,我希望显示Facebook非页内广告,但不获取结果,仅显示带有按钮的对话框而未加载广告?

如何在对话框中加载Facebook非页内广告?

这是对话框布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/facebook_interestial_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingTop="4dp"
    android:paddingBottom="4dp">
    <LinearLayout
        android:id="@+id/facebook_interestial_ad"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:orientation="vertical">

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3.2">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="12dp"
            android:layout_weight="1.8"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:id="@+id/textView9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Are you sure to exit app ?"
                android:textSize="14sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:orientation="vertical">

            <Button
                android:id="@+id/yes_exit_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="YES" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.7"
            android:orientation="vertical">

            <Button
                android:id="@+id/no_exit_btn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:text="NO" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

这是“查看对话框”类

    import com.facebook.ads.AbstractAdListener;
import com.facebook.ads.Ad;

public class ViewDialog {

    com.facebook.ads.InterstitialAd interstitial;
    Ad adfacebook;
        public void showDialog(Activity activity) {
            Context c1= activity.getBaseContext();
            final Dialog dialog = new Dialog(activity);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(false);
            dialog.setContentView(R.layout.fbdialogexit);


            LinearLayout adlayout = (LinearLayout)dialog.findViewById(R.id.facebook_interestial_ad);



            interstitial = new com.facebook.ads.InterstitialAd(c1, "your id");
        interstitial.setAdListener(new AbstractAdListener() {
            public void onAdLoaded(Ad ad) {
                adfacebook = ad;


            }
        });
        interstitial.loadAd();

            Button dialogExitCancelButton = (Button) dialog.findViewById(R.id.no_exit_btn);
            dialogExitCancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });


            Button dialogExitButton = (Button) dialog.findViewById(R.id.yes_exit_btn);
            dialogExitButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   System.exit(0);
                }
            });

            dialog.show();

        }


}

回叫主活动按按钮

  @Override
public void onBackPressed() { ViewDialog alert = new ViewDialog();
    alert.showDialog(StartActivity.this);

}

0 个答案:

没有答案