首先,几天前有些广告还有效,但今天我发现它们停止了,我不知道我是否改变了什么。
它们在任何设备上都不起作用,我也没有使用测试广告
在 Android Studio 的测试设备中,它们在顶部显示了一个小文本,上面写着“测试广告”(我使用我的 admob id,所以这不是问题)但是当我制作 APK 并在设备广告根本不显示,甚至连测试广告都没有,它们就消失了,让我告诉你我的意思:
在测试虚拟设备中: https://gyazo.com/704e18c00544794ec4e29faad1a1ab8d
在实际设备中: https://imgur.com/a/iERofay
我会在这里放一些代码,但我不知道我应该在这里放什么代码,所以我让你们告诉我要发送什么,这样你们就可以看到问题所在。
所有与广告有关的文件都是 gif 中的这些文件: https://gyazo.com/8ca6c2b10e9b392c424f641ffaefb26a
(告诉我你要我发送什么文件,我会发送到这里)
(抱歉英语不好,我来自西类牙)
我用///////审查了广告 ID 和应用程序 ID!
列出 Activity
package com.example.samplestickerapp;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdListener;
public class StickerPackListActivity extends AddStickerPackActivity {
private PublisherAdView mPublisherAdView;
private InterstitialAd mInterstitialAd;
public static final String EXTRA_STICKER_PACK_LIST_DATA = "sticker_pack_list";
private static final int STICKER_PREVIEW_DISPLAY_LIMIT = 5;
private LinearLayoutManager packLayoutManager;
private RecyclerView packRecyclerView;
private StickerPackListAdapter allStickerPacksListAdapter;
private WhiteListCheckAsyncTask whiteListCheckAsyncTask;
private ArrayList<StickerPack> stickerPackList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sticker_pack_list);
MobileAds.initialize(this, "ca-app-pub-///////////~/////////");
mPublisherAdView = findViewById(R.id.publisherAdView);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mPublisherAdView.loadAd(adRequest);
packRecyclerView = findViewById(R.id.sticker_pack_list);
stickerPackList = getIntent().getParcelableArrayListExtra(EXTRA_STICKER_PACK_LIST_DATA);
showStickerPackList(stickerPackList);
}
@Override
protected void onResume() {
super.onResume();
whiteListCheckAsyncTask = new WhiteListCheckAsyncTask(this);
whiteListCheckAsyncTask.execute(stickerPackList.toArray(new StickerPack[stickerPackList.size()]));
}
@Override
protected void onPause() {
super.onPause();
if (whiteListCheckAsyncTask != null && !whiteListCheckAsyncTask.isCancelled()) {
whiteListCheckAsyncTask.cancel(true);
}
}
private void showStickerPackList(List<StickerPack> stickerPackList) {
allStickerPacksListAdapter = new StickerPackListAdapter(stickerPackList, onAddButtonClickedListener);
packRecyclerView.setAdapter(allStickerPacksListAdapter);
packLayoutManager = new LinearLayoutManager(this);
packLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
packRecyclerView.getContext(),
packLayoutManager.getOrientation()
);
packRecyclerView.addItemDecoration(dividerItemDecoration);
packRecyclerView.setLayoutManager(packLayoutManager);
packRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(this::recalculateColumnCount);
}
private final StickerPackListAdapter.OnAddButtonClickedListener onAddButtonClickedListener = pack -> {
addStickerPackToWhatsApp(pack.identifier, pack.name);
};
private void recalculateColumnCount() {
final int previewSize = getResources().getDimensionPixelSize(R.dimen.sticker_pack_list_item_preview_image_size);
int firstVisibleItemPosition = packLayoutManager.findFirstVisibleItemPosition();
StickerPackListItemViewHolder viewHolder = (StickerPackListItemViewHolder) packRecyclerView.findViewHolderForAdapterPosition(firstVisibleItemPosition);
if (viewHolder != null) {
final int max = Math.max(viewHolder.imageRowView.getMeasuredWidth() / previewSize, 1);
int numColumns = Math.min(STICKER_PREVIEW_DISPLAY_LIMIT, max);
allStickerPacksListAdapter.setMaxNumberOfStickersInARow(numColumns);
}
}
static class WhiteListCheckAsyncTask extends AsyncTask<StickerPack, Void, List<StickerPack>> {
private final WeakReference<StickerPackListActivity> stickerPackListActivityWeakReference;
WhiteListCheckAsyncTask(StickerPackListActivity stickerPackListActivity) {
this.stickerPackListActivityWeakReference = new WeakReference<>(stickerPackListActivity);
}
@Override
protected final List<StickerPack> doInBackground(StickerPack... stickerPackArray) {
final StickerPackListActivity stickerPackListActivity = stickerPackListActivityWeakReference.get();
if (stickerPackListActivity == null) {
return Arrays.asList(stickerPackArray);
}
for (StickerPack stickerPack : stickerPackArray) {
stickerPack.setIsWhitelisted(WhitelistCheck.isWhitelisted(stickerPackListActivity, stickerPack.identifier));
}
return Arrays.asList(stickerPackArray);
}
@Override
protected void onPostExecute(List<StickerPack> stickerPackList) {
final StickerPackListActivity stickerPackListActivity = stickerPackListActivityWeakReference.get();
if (stickerPackListActivity != null) {
stickerPackListActivity.allStickerPacksListAdapter.setStickerPackList(stickerPackList);
stickerPackListActivity.allStickerPacksListAdapter.notifyDataSetChanged();
}
}
}
}
Android list
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.samplestickerapp">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="false"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".StickerApplication"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".EntryActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StickerPackListActivity"
android:label="@string/title_activity_sticker_packs_list" />
<activity
android:name=".StickerPackDetailsActivity"
android:parentActivityName=".StickerPackListActivity"
tools:ignore="UnusedAttribute">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.samplestickerapp.StickerPackListActivity" />
</activity>
<activity
android:name=".StickerPackInfoActivity"
android:label="@string/title_activity_sticker_pack_info"
android:parentActivityName=".StickerPackDetailsActivity"
tools:ignore="UnusedAttribute">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.samplestickerapp.StickerPackDetailsActivity" />
</activity>
<provider
android:name=".StickerContentProvider"
android:authorities="${contentProviderAuthority}"
android:enabled="true"
android:exported="true"
android:readPermission="com.whatsapp.sticker.READ" />
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
<meta-data
android:name="com.google.android.gms.ads.ca-app-pub-/////////////~//////////"
android:value="ca-app-pub-/////////////~//////////"/>
</application>
</manifest>
最佳答案
在您创建您的 admob 帐户并添加您的应用程序后,您将获得您在 list 中使用的应用程序 ID 以及用于初始化广告。
现在您需要从您的 adMob 帐户为您的添加创建添加单元,并将您的添加单元 ID 与您在设备中显示的 addView 相关联。
如果您使用了网站的默认代码,您可能正在使用 id 测试广告单元。您需要将其替换为您的广告单元 ID。
一旦您发布了您的应用,您需要将您在 Google Play 上的应用与您的 adMob 帐户相关联。
最后,添加内容只会在互联网连接可用时显示。
根据评论编辑
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value = "your-application-id-goes-here"/>
Make sure you have updated AdMob with your payment details Make sure that the ads you created in AdMob are banner ads. Check your AdMob dashboard to see the status of your ads, are they active? Verify you used the correct Ad Unit Id. Give it 24 hours, it can take time for an ad to become active in your region
请尝试遵循指南 here .
检查代码中的以下几行
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mPublisherAdView.loadAd(adRequest);
改为使用 ((AdView)findViewById(R.id.adView)).loadAd(new AdRequest.Builder().build());
关于android - 为什么 Admob 广告没有显示在实际设备上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53571653/