MobileAds.initialize(上下文,字符串) 已弃用
MobileAds.initialize(Context, String) is deprecated
以前是这样的(通过 R.string.admob_application_id
):
MobileAds.initialize(this, getString(R.string.admob_application_id));
MobileAds
方法.initialize(Context context, String applicationCode)
现在阅读:
This method is deprecated. Use initialize(Context, OnInitializationCompleteListener)
instead.
上面写着:
Initializes the Google Mobile Ads SDK.
Call this method as early as possible to reduce latency on the session's first ad request.
If this method is not called, the first ad request automatically initializes the Google Mobile Ads SDK.
MobileAds.Settings 也已弃用。
到目前为止我尝试的是为每个适配器获取 InitializationStatus
:
MobileAds.initialize(this, initializationStatus -> {
/* get the adapter status */
Map<String, AdapterStatus> map = initializationStatus.getAdapterStatusMap();
for (Map.Entry<String, AdapterStatus> entry : map.entrySet()) {
AdapterStatus adapterStatus = entry.getValue();
AdapterStatus.State state = adapterStatus.getInitializationState();
Log.d(LOG_TAG, "key = " + entry.getKey() + ", state = " + state.name() + ", desc = " + adapterStatus.getDescription());
}
});
在模拟器上显示 READY
,但 .getDescription()
有一个相当混乱的消息:
Google Mobile Ads SDK initialization functionality unavailable for this session.
Ad requests can be made at any time.
在具有互联网连接的物理设备上,它显示另外四个适配器:
key = com.google.android.gms.ads.MobileAds, state = READY, desc =
key = com.google.ads.mediation.vungle.VungleMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.applovin.AppLovinMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.adcolony.AdColonyMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.tapjoy.TapjoyMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
om.google.android.gms.ads.MobileAds
读作READY
,另一个com.google.ads.mediation.*
读作NOT_READY
和Failed to create Adapter
(没有mediation设置,但是它可以加载测试广告)。
MobileAds.initialize(this);
也有效(OnInitializationCompleteListener
是可选的)。
它似乎在没有 R.string.admob_application_id
的情况下工作,因此我的实际问题是:
是否仍然需要设置 R.string.admob_publisher_id
和 R.string.admob_application_id
- 或者现在只是广告单元 ID?大多数文档仍然使用已弃用的初始化方法(如上所示),因此这并不清楚。
Get started with AdMob in your Android project 似乎回答了应用程序 ID 部分:
Add your AdMob App ID to your app's AndroidManifest.xml
file by adding the <meta-data>
tag as shown below.
Important: This step is required as of Google Mobile Ads SDK v17.0.0. If you don't add this <meta-data>
tag, your app will crash with the message: "The Google Mobile Ads SDK was initialized incorrectly.
"
<manifest>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ADMOB_APP_ID"/>
</application>
</manifest>
并且 Requesting Consent from European Users 仍然需要发布者 ID。
改为使用以下内容初始化 MobileAds:
MobileAds.initialize(this, initializationStatus -> { });
以前是这样的(通过 R.string.admob_application_id
):
MobileAds.initialize(this, getString(R.string.admob_application_id));
MobileAds
方法.initialize(Context context, String applicationCode)
现在阅读:
This method is deprecated. Use
initialize(Context, OnInitializationCompleteListener)
instead.
上面写着:
Initializes the Google Mobile Ads SDK. Call this method as early as possible to reduce latency on the session's first ad request. If this method is not called, the first ad request automatically initializes the Google Mobile Ads SDK.
MobileAds.Settings 也已弃用。
到目前为止我尝试的是为每个适配器获取 InitializationStatus
:
MobileAds.initialize(this, initializationStatus -> {
/* get the adapter status */
Map<String, AdapterStatus> map = initializationStatus.getAdapterStatusMap();
for (Map.Entry<String, AdapterStatus> entry : map.entrySet()) {
AdapterStatus adapterStatus = entry.getValue();
AdapterStatus.State state = adapterStatus.getInitializationState();
Log.d(LOG_TAG, "key = " + entry.getKey() + ", state = " + state.name() + ", desc = " + adapterStatus.getDescription());
}
});
在模拟器上显示 READY
,但 .getDescription()
有一个相当混乱的消息:
Google Mobile Ads SDK initialization functionality unavailable for this session.
Ad requests can be made at any time.
在具有互联网连接的物理设备上,它显示另外四个适配器:
key = com.google.android.gms.ads.MobileAds, state = READY, desc =
key = com.google.ads.mediation.vungle.VungleMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.applovin.AppLovinMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.adcolony.AdColonyMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
key = com.google.ads.mediation.tapjoy.TapjoyMediationAdapter, state = NOT_READY, desc = Failed to create Adapter.
om.google.android.gms.ads.MobileAds
读作READY
,另一个com.google.ads.mediation.*
读作NOT_READY
和Failed to create Adapter
(没有mediation设置,但是它可以加载测试广告)。
MobileAds.initialize(this);
也有效(OnInitializationCompleteListener
是可选的)。
它似乎在没有 R.string.admob_application_id
的情况下工作,因此我的实际问题是:
是否仍然需要设置 R.string.admob_publisher_id
和 R.string.admob_application_id
- 或者现在只是广告单元 ID?大多数文档仍然使用已弃用的初始化方法(如上所示),因此这并不清楚。
Get started with AdMob in your Android project 似乎回答了应用程序 ID 部分:
Add your AdMob App ID to your app's
AndroidManifest.xml
file by adding the<meta-data>
tag as shown below.Important: This step is required as of Google Mobile Ads SDK v17.0.0. If you don't add this
<meta-data>
tag, your app will crash with the message: "The Google Mobile Ads SDK was initialized incorrectly.
"
<manifest>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ADMOB_APP_ID"/>
</application>
</manifest>
并且 Requesting Consent from European Users 仍然需要发布者 ID。
改为使用以下内容初始化 MobileAds:
MobileAds.initialize(this, initializationStatus -> { });