如何在返回按下Android按钮时显示插页式广告

时间:2017-02-17 11:00:46

标签: android ads android-fragmentactivity interstitial

Hye朋友们!关于我的Android应用,我需要你们的帮助。基本上我的android项目是片段选项卡,我想在关闭应用程序之前用户点击后退按钮时显示非页内广告。

请查看下面的编码,并帮助我尽快解答这个问题:)

MainActivity.java

public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/01"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/02"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/03"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/04"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/05"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/06"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/07"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/08"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/09"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/10"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/11"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/12"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/13"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/14"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/15"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/16"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/17"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/18"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/19"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/20"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/21"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/22"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/23"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/24"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/25"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/26"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/27"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/28"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/29"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/30"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/31"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/32"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/33"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/34"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/35"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/36"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/37"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/38"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/39"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/40"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/41"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/42"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/43"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/44"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/45"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/46"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/47"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/48"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/49"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/50"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/51"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/52"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/53"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/54"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/55"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/56"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/57"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/58"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/59"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/60"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/61"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/62"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/63"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/64"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/65"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/66"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/67"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/68"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/69"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/70"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/71"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/72"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/73"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/74"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/75"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/76"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/77"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/78"));
    tabLayout.addTab(tabLayout.newTab().setText("Ashaar/79"));


    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);


    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }


    });
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}



public void onBackPressed() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            MainActivity.this);

    // set title
    alertDialogBuilder.setTitle("Exit");

    // set dialog message
    alertDialogBuilder
            .setMessage("Do you really want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    MainActivity.this.finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();
}

public void sharevia(MenuItem item) {
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "https://play.google.com/store/apps/details?id=com.islamichub.dil.jalay";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "https://play.google.com/store/apps/details?id=com.islamichub.dil.jalay");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

public void rating(MenuItem item) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.islamichub.dil.jalay")));
}

public void findmore(MenuItem item) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pub:IslamicHub")));
}

public void aboutme(MenuItem item) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.fb.com/topitideas")));
}

activity_main.xml中

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    app:titleTextColor="#ffffff"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimaryDark"
    android:elevation="6dp"
    app:tabSelectedTextColor="#ffff00"
    app:tabTextColor="#000000"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:tabMode="scrollable"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout" />

2 个答案:

答案 0 :(得分:2)

请在onCreate()方法中加载广告实例。因此,您可以在不加载的情况下预取广告。 写下来就像..

@Override
public void onBackPressed() {
    showAdvertisement();
}

private void showAdvertisement() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    } else {
        finish();
    }
}

private void loadInterstitialAd() {
    AdRequest adRequest = new AdRequest.Builder()
            .build();

    mInterstitialAd.loadAd(adRequest);
}

答案 1 :(得分:1)

我假设您正站在主要活动上并且堆栈中没有任何活动。您需要覆盖onBackPressed()方法并在onBackPressed()中添加用于间隔添加的代码

@Override
public void onBackPressed() {
//Interstitial code goes here
}

有关添加插页式广告代码或代码帮助的信息,请参阅以下链接 https://firebase.google.com/docs/admob/android/interstitial