Java 安卓:Admob州际广告将不显示

Java 安卓:Admob州际广告将不显示,java,android,admob,Java,Android,Admob,我是Android开发领域的新手,我一直在通过各种教程和自学来构建一个音板应用程序。我已经让这个应用程序运行得相当好,并且到目前为止已经有了一个横幅测试广告 我希望每按5次就能看到一个州际广告,不过我只是想看看我是否能触发州际广告,所以现在的应用程序是如何编码的,可以在任何按键上显示 我似乎无法得到一个州际广告显示,虽然我认为它是加载 有关于如何最好地调查/解决此问题的建议吗 (由于字符限制,无法添加logcat。) 非常感谢, 主要活动 package com.FoxOne.BritishFa

我是Android开发领域的新手,我一直在通过各种教程和自学来构建一个音板应用程序。我已经让这个应用程序运行得相当好,并且到目前为止已经有了一个横幅测试广告

我希望每按5次就能看到一个州际广告,不过我只是想看看我是否能触发州际广告,所以现在的应用程序是如何编码的,可以在任何按键上显示

我似乎无法得到一个州际广告显示,虽然我认为它是加载

有关于如何最好地调查/解决此问题的建议吗

(由于字符限制,无法添加logcat。)

非常感谢,

主要活动

package com.FoxOne.BritishFartsSoundboard;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入androidx.appcompat.app.appcompat活动;
导入androidx.appcompat.widget.SearchView;
导入androidx.appcompat.widget.Toolbar;
导入androidx.core.app.ActivityCompat;
导入androidx.core.content.ContextCompat;
导入androidx.loader.app.LoaderManager;
导入androidx.loader.content.loader;
导入androidx.recyclerview.widget.GridLayoutManager;
导入androidx.recyclerview.widget.recyclerview;
导入android.Manifest;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.pm.PackageManager;
导入android.net.Uri;
导入android.os.Build;
导入android.os.Bundle;
导入android.provider.Settings;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Toast;
导入com.google.android.gms.ads.AdListener;
导入com.google.android.gms.ads.AdSize;
导入com.google.android.gms.ads.alad;
导入com.google.android.gms.ads.loaderror;
导入com.google.android.gms.ads.MobileAds;
导入com.google.android.gms.ads.RequestConfiguration;
导入com.google.android.gms.ads.initialization.InitializationStatus;
导入com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
导入com.google.android.gms.ads.AdRequest;
导入com.google.android.gms.ads.AdView;
导入com.google.android.material.snackbar.snackbar;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公开课声板活动
扩展AppCompatActivity实现LoaderManager.LoaderCallbacks{
//ADMOB
私人咨询咨询;
@SuppressLint(“错误视图投射”)
/**离开活动时调用*/
@凌驾
公共无效暂停(){
if(adView!=null){
暂停;
}
super.onPause();
}
/**返回活动时调用*/
@凌驾
恢复时公开作废(){
super.onResume();
if(adView!=null){
adView.resume();
}
}
//结束ADMOB
/**
*主活动。膨胀主活动xml和子片段。
*/
//定义用于记录任何类型错误或注释的标记
私有静态最终字符串LOG_TAG=SoundboardActivity.class.getSimpleName();
//声明要使用的mToolbar,而不是系统标准mToolbar
私有工具栏mToolbar;
//声明用SoundObjects填充的ArrayList
private ArrayList mSoundList=new ArrayList();
//声明RecyclerView及其组件
//您可以立即分配RecyclerView.适配器
私人回收视图mRecyclerView;
私人音板回收器适配器mRecyclerAdapter;
private RecyclerView.LayoutManager MLLayoutManager;
//声明一个视图,该视图将包含活动的布局,并用作Snackbar的父级
私有视图布局;
//声明DatabaseHandler以支持数据库使用
私有数据库处理程序mDatabaseHandler;
//宣布ADMOB为间质
私人间质;
私有静态最终字符串AD_UNIT_ID=“ca-app-pub-3940256099942544/1033173712”;
//结束ADMOB
@SuppressLint(“错误视图投射”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_音板);
//将实例分配给DatabaseHandler
mDatabaseHandler=DatabaseHandler.getInstance(this);
//如果获取更新或第一次运行,请使用所有SoundObjects填充数据库
if(appUpdate()){
mDatabaseHandler.createSoundCollection();
mDatabaseHandler.updateFavourites();
}
//指定布局视图
//查看activity_soundboard.xml更改id
mLayout=findViewById(R.id.activity\U音板);
//将mToolbar分配给activity_soundboard.xml中声明的工具栏项
mToolbar=(工具栏)findViewById(R.id.soundboard\u工具栏);
//将mToolbar设置为新操作栏
设置支持操作栏(mToolbar);
//将mRecyclerView分配给在activity_soundboard.xml中声明的RecyclerView项
mRecyclerView=(RecyclerView)findViewById(R.id.soundboard\u RecyclerView);
//将RecyclerView.LayoutManager定义为具有3列
mLayoutManager=新的GridLayoutManager(这个,3);
//设置RecyclerView.LayoutManager
mRecyclerView.setLayoutManager(mllayoutmanager);
//初始化回收器适配器
mRecyclerAdapter=新的SoundboardRecyclerAdapter(此,mSoundList);
//设置RecyclerView.Adapter
mRecyclerView.setAdapter(mRecyclerAdapter);
//调用处理所有权限事件的方法
请求权限();
//调用将数据库中的数据添加到mSoundList的方法
getSupportLoaderManager().initLoader(R.id.common\u soundlist\u loader\u id,null,this);
//ADMOB旗
//初始化移动ADSSDK。
初始化(这是新的OnInitializationCompleteListener(){
@凌驾
public void onInitializationComplete(初始化状态InitializationStatus
package com.FoxOne.BritishFartsSoundboard;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class SoundboardActivity
        extends AppCompatActivity implements LoaderManager.LoaderCallbacks<ArrayList<SoundObject>> {



    // ADMOB
    private AdView adView;
    @SuppressLint("WrongViewCast")

    /** Called when leaving the activity */
    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    /** Called when returning to the activity */
    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

// END ADMOB


    /**
     * Main Activity. Inflates main activity xml and child fragments.
     */


// Define a tag that is used to log any kind of error or comment
private static final String LOG_TAG = SoundboardActivity.class.getSimpleName();

// Declare a mToolbar to use instead of the system standard mToolbar
private Toolbar mToolbar;

// Declare an ArrayList that you fill with SoundObjects
private ArrayList<SoundObject> mSoundList = new ArrayList<>();

// Declare a RecyclerView and its components
// You can assign the RecyclerView.Adapter right away
private RecyclerView mRecyclerView;
private SoundboardRecyclerAdapter mRecyclerAdapter;
private RecyclerView.LayoutManager mLayoutManager;

// Declare a View that will contain the layout of the activity and serves as the parent of a Snackbar
private View mLayout;

// Declare a DatabaseHandler to support database usage
private DatabaseHandler mDatabaseHandler;


// DECLARE ADMOB INTERSTITIAL
private InterstitialAd mInterstitialAd;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";

// END ADMOB


@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_soundboard);

        // Assign an instance to the DatabaseHandler
        mDatabaseHandler = DatabaseHandler.getInstance(this);

        // If the gets an update or runs for the first time fill the database with all SoundObjects
        if (appUpdate()) {

        mDatabaseHandler.createSoundCollection();

        mDatabaseHandler.updateFavourites();
        }

        // Assign layout view
        // Take a look at activity_soundboard.xml change the id
        mLayout = findViewById(R.id.activity_soundboard);

        // Assign mToolbar to the Toolbar item declared in activity_soundboard.xml
        mToolbar = (Toolbar) findViewById(R.id.soundboard_toolbar);

        // Set mToolbar as new action bar
        setSupportActionBar(mToolbar);

        // Assign mRecyclerView to the RecyclerView item declared in activity_soundboard.xml
        mRecyclerView = (RecyclerView) findViewById(R.id.soundboard_RecyclerView);

        // Define the RecyclerView.LayoutManager to have 3 columns
        mLayoutManager = new GridLayoutManager(this, 3);

        // Set the RecyclerView.LayoutManager
        mRecyclerView.setLayoutManager(mLayoutManager);

        // Initialize recycler adapter
        mRecyclerAdapter = new SoundboardRecyclerAdapter(this, mSoundList);

        // Set the RecyclerView.Adapter
        mRecyclerView.setAdapter(mRecyclerAdapter);

        // Calls a method that handles all permission events
        requestPermissions();

        // Calls a method that adds data from a database to the mSoundList
        getSupportLoaderManager().initLoader(R.id.common_soundlist_loader_id, null, this);


       //ADMOB BANNER

    // Initialize the Mobile Ads SDK.
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
    });

    // Set your test devices. Check your logcat output for the hashed device ID to
    // get test ads on a physical device. e.g.
    // "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
    // to get test ads on this device."
    MobileAds.setRequestConfiguration(
            new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("E6B987D1F47C6213E36D3F8896AA4DE8",AdRequest.DEVICE_ID_EMULATOR))
                    .build());

    // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
    // values/strings.xml.
    adView = findViewById(R.id.adView);

    //Banner Advert Size
    AdSize adSize = new AdSize(320, 50);

    // Create an ad request.
    AdRequest adRequest = new AdRequest.Builder().build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
    // END ADMOB BANNER


    // ADMOB INTERSTITIAL
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(AD_UNIT_ID );
    //Create Ad Request
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    // Begin loading your interstitial.
    new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("E6B987D1F47C6213E36D3F8896AA4DE8",AdRequest.DEVICE_ID_EMULATOR))
            .build();
    List <String> testDeviceIds = Arrays.asList("E6B987D1F47C6213E36D3F8896AA4DE8");
    RequestConfiguration configuration =
            new RequestConfiguration.Builder().setTestDeviceIds(testDeviceIds).build();
    MobileAds.setRequestConfiguration(configuration);

    // ADMOB INTERSTITIAL END


// ADMOB INTERSTITIAL
    mInterstitialAd.setAdListener(
            new AdListener() {
                @Override
                public void onAdLoaded() {
                    Toast.makeText(SoundboardActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
                    System.out.println("onAdLoaded() CALLBACK");
                }

                @Override
                public void onAdFailedToLoad(LoadAdError loadAdError) {
                    String error =
                            String.format(
                                    "domain: %s, code: %d, message: %s",
                                    loadAdError.getDomain(), loadAdError.getCode(), loadAdError.getMessage());
                    System.out.println("onAdFailedToLoad() CALLBACK");
                    Toast.makeText(SoundboardActivity.this, "onAdFailedToLoad() with error: " + error, Toast.LENGTH_SHORT)
                            .show();
                }

                @Override
                public void onAdOpened() {
                    // Code to be executed when the ad is displayed.
                    Toast.makeText(SoundboardActivity.this, "onAdOpened()", Toast.LENGTH_SHORT).show();
                    System.out.println("onAdLoaded() CALLBACK");
                }
                @Override
                public void onAdClosed() {
                    // Load the next interstitial.
                    System.out.println("onAdClosed() CALLBACK");
                    mInterstitialAd.loadAd(new AdRequest.Builder().build());}
                                });
    // ADMOB INTERSITIAL END

        };

// Create an options menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the layout
        getMenuInflater().inflate(R.menu.toolbar_menu, menu);

        // Define a SearchView to provide a search function and define its behaviour
        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {

        /*
         * Searches for sound objects that begin with the
         * entered string and displays them on submit of the search.
         */
        mRecyclerAdapter.queryData(query);
        return true;
        }

@Override
public boolean onQueryTextChange(String newText) {
        return false;
        }
        });

        menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
        }

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {

        getSupportLoaderManager()
        .restartLoader(R.id.common_soundlist_loader_id,
        null,
        SoundboardActivity.this);
        return true;
        }
        });

        return super.onCreateOptionsMenu(menu);
        }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.action_favorite_show) {
        this.startActivity(new Intent(this, FavouriteActivity.class));
        }

        switch (item.getItemId()) {

        case R.id.action_favourite:
        startActivity(new Intent(this, FavouriteActivity.class));
        break;
default:
        }

        return super.onOptionsItemSelected(item);
        }

// Takes care of some things when the user closes the activity
@Override
protected void onDestroy() {
        super.onDestroy();

        // Calls a method that releases all data from the used MediaPlayer instance
        EventHandlerClass.releaseMediaPlayer();

        //Admob
    if (adView != null) {
        adView.destroy();
    }
    super.onDestroy();
        // End Admob
        }

    /** Called before the activity is destroyed */



    /**
 * Handles all permission events on startup.
 */


private void requestPermissions() {

        // Check if the users Android version is equal to or higher than Android 6 (Marshmallow)
        // Since Android 6 you have to request permissions at runtime to provide a better security
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        // Check if the permission to write and read the users external storage is not granted
        // You need this permission if you want to share sounds via WhatsApp or the like
        if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {

        // If the permission is not granted request it
        ActivityCompat.requestPermissions(this,
        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
        0);
        }

        // Check if the permission to write the users settings is not granted
        // You need this permission to set a sound as ringtone or the like
        if (!Settings.System.canWrite(this)) {

        /*
         * Searches for sound objects that begin with the
         * entered string and displays them on submit of the search.
         */

        Snackbar.make(mLayout, "The app needs access to your settings", Snackbar.LENGTH_INDEFINITE)
        .setAction("OK",




         new View.OnClickListener() {

             private void startInterstitial() {
                 // Request a new ad if one isn't already loaded.
                 if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
                     AdRequest adRequest = new AdRequest.Builder().build();
                     mInterstitialAd.loadAd(adRequest);
                 }

             }
             private void showInterstitial() {
                 // Show the ad if it's ready. Otherwise toast and restart.
                 if (mInterstitialAd.isLoaded() ) {
                     mInterstitialAd.show();
                 } else {
                     Toast.makeText(SoundboardActivity.this, "Ad did not load", Toast.LENGTH_SHORT).show();
                     startInterstitial();

                 }
             }



             public int counter = 0;


@Override
// Define a tag that is used to log any kind of error or comment



public void onClick(View v) {

    counter++;

    Context context = v.getContext();
    Toast.makeText(SoundboardActivity.this, "Current Counter:"+ String.valueOf(counter) , Toast.LENGTH_SHORT).show(); //This doesn't show
    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
    intent.setData(Uri.parse("package:" + context.getPackageName()));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    //startInterstitial();


    if (counter == counter) { //Change back to 5 after testing


        //run process for interstitial
        showInterstitial();
    }else{
        Log.d("TAG", "The interstitial wasn't loaded yet.");
        System.out.println("Interstital wasn't loaded () CALLBACK");
            }

    }

}).show();

        }

        }
        }

/**
 * Checks if the app has been updated by looking at the versionCode defined in the modules build.gradle.
 *
 * @return True if the app has been updated.
 */
private boolean appUpdate() {



/*
 * We are saving the current app version into a preference file.
 * There are two ways to get a handle to a SharedPreferences,
 * we are creating a unique preference file that is not bound to a context.
 * Check the android developer documentation if you want to find out more.
 */

// Define a name for the preference file and a key name to save the version code to it
final String prefsName = "VersionPref";
final String prefVersionCodeKey = "version_code";
// Define a value that is set if the key does not exist
final int doesntExist = -1;

        // Get the current version code from the package
        int currentVersionCode = 0;
        try {

        currentVersionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;

        } catch (PackageManager.NameNotFoundException e) {

        Log.e(LOG_TAG, e.getMessage());
        }

        // Get the SharedPreferences from the preference file
        // Creates the preference file if it does not exist
        SharedPreferences prefs = getSharedPreferences(prefsName, MODE_PRIVATE);
        // Get the saved version code or set it if it does not exist
        int savedVersionCode = prefs.getInt(prefVersionCodeKey, doesntExist);

        // Create an editor to edit the shared preferences on app update
        SharedPreferences.Editor edit = prefs.edit();

        //Check for updates
        if (savedVersionCode == doesntExist) {

        mDatabaseHandler.appUpdate();
        // First run of the app
        // Set the saved version code to the current version code
        edit.putInt(prefVersionCodeKey, currentVersionCode);
        edit.apply();
        return true;
        } else if (currentVersionCode > savedVersionCode) {

        // App update
        mDatabaseHandler.appUpdate();
        edit.putInt(prefVersionCodeKey, currentVersionCode);
        edit.apply();
        return true;
        }

        return false;
        }

@NonNull
@Override
public Loader<ArrayList<SoundObject>> onCreateLoader(int id, @Nullable Bundle args) {
        return new SoundListLoader(getApplicationContext()) {

@Override
public ArrayList<SoundObject> loadInBackground() {
        return DatabaseHandler.getInstance(SoundboardActivity.this).getSoundCollection();
        }
        };
        }

@Override
public void onLoadFinished(@NonNull Loader<ArrayList<SoundObject>> loader, ArrayList<SoundObject> data) {

        mRecyclerAdapter.swapData(data);
        }

@Override
public void onLoaderReset(@NonNull Loader<ArrayList<SoundObject>> loader) {
        mRecyclerAdapter.swapData(new ArrayList<SoundObject>());
        }


        }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_height="match_parent"
    tools:context=".SoundboardActivity"
    android:background="@color/grey"
    android:id="@+id/activity_soundboard">

    <include
        layout="@layout/toolbar"
        android:id="@+id/soundboard_toolbar" />


    <!-- Admob -->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/soundboard_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/soundboard_toolbar"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/adView"
        >

    </androidx.recyclerview.widget.RecyclerView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:visibility="visible"
        app:adSize="BANNER"
        app:adUnitId="@string/ad_banner_id"
        tools:visibility="visible"
         />


    <!-- Admob -->

</RelativeLayout>

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-Your-app-id~must-be-here-provided-by-admob" />