各位知道其他路子的求分享啊灾部,我實在想不到什么方法了绷蹲,可惜谷歌被屏蔽了堂油,但是總比不能賺1毛錢好修档。我相信我的粉絲都是極客,
注冊admob
快速開始教程
https://developers.google.com/admob/android/quick-start?hl=zh-CN
創(chuàng)建應(yīng)用
創(chuàng)建banner
得到Y(jié)OUR_ADMOB_APP_ID
https://developers.google.com/admob/android/quick-start?hl=zh-CN#import_the_mobile_ads_sdk
添加xml代碼
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-6391307239504132/6102840431" />
添加java代碼
package ...
import ...
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//全局appid
MobileAds.initialize(this, "ca-app-pub-6391307239504132~6124388718");
// MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
adView = findViewById(R.id.ad_view);
// 建立發(fā)送廣告請求
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// 添加廣告監(jiān)聽.
adView.setAdListener(new AdListener(){
@Override
public void onAdFailedToLoad(int i) {
Log.w(TAG,"load fail :"+i);
}
});
//加載請求
adView.loadAd(adRequest); }
}
}
橫幅實現(xiàn)的官方教程在這里
https://developers.google.com/admob/android/banner?hl=zh-CN
或者用代碼添加banner的方法
adView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-6391307239504132/6102840431");
規(guī)范細(xì)節(jié)
不要忘記代理一下聲明周期府框。
/** Called when returning to the activity */
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
/** Called before the activity is destroyed */
@Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
各位測試沒效果的可以先直接把我的代碼copy測試效果如何吱窝。
插屏廣告
InterstitialAd mInterstitialAd = new InterstitialAd(this);//插頁廣告
mInterstitialAd.setAdUnitId("xxxxx");
激勵廣告
RewardedVideoAd mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
mRewardedVideoAd.loadAd("ca-app-pub-6391307239504132/8306864775", new AdRequest.Builder().build());
AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
@Override
public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
// Show the app install ad.
}
})
.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
@Override
public void onContentAdLoaded(NativeContentAd contentAd) {
// Show the content ad.
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, and so on.
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
adLoader.loadAd(new AdRequest.Builder().build());
原生廣告
說實話原生廣告的,老美的文檔根本不全迫靖,有點亂院峡,根本不知道某個東西如何來的。比如
private void displayAppInstallAd(ViewGroup parent,NativeAppInstallAd ad) {
// Inflate a layout and add it to the parent ViewGroup.
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
NativeAppInstallAdView adView = (NativeAppInstallAdView) inflater
.inflate(R.layout.my_ad_layout, parent);
// Locate the view that will hold the headline, set its text, and call the
// NativeAppInstallAdView's setHeadlineView method to register it.
TextView headlineView = adView.findViewById(R.id.ad_headline);
headlineView.setText(ad.getHeadline());
adView.setHeadlineView(headlineView);
// Repeat the above process for the other assets in the NativeAppInstallAd
// If the app is using a MediaView to display video, it should be
// instantiated and passed to setMediaView. This view is a little different
// in that the asset is populated automatically, so there's one less step.
MediaView mediaView = (MediaView) adView.findViewById(R.id.ad_media);
adView.setMediaView(mediaView);
// Call the NativeAppInstallAdView's setNativeAd method to register the
// NativeAdObject.
adView.setNativeAd(ad);
// Place the AdView into the parent.
parent.addView(adView);
}
這里需要傳遞一個參數(shù)系宜,文檔并沒有說是怎么出來的照激,我感覺需要找找他們的demo.
https://developers.google.com/admob/android/native-advanced?hl=zh-CN
https://apps.admob.com/v2/home
將 Firebase 添加到您的 Android 項目
https://firebase.google.com/docs/android/setup
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:3.2.0' // google-services plugin
}
}
allprojects {
// ...
repositories {
// ...
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
完整步驟
請使用此應(yīng)用 ID 完成 Google 移動廣告 SDK 指南中的說明:
請按照激勵廣告實現(xiàn)指南來集成 SDK。在使用此廣告單元 ID 集成代碼時盹牧,您需要指定廣告類型和展示位置:
- 請點擊此處開始從 Firebase 控制臺下載配置文件俩垃。在下一步的最后,您將需要用到此文件汰寓。如果您無法訪問 Firebase 控制臺口柳,請與 Firebase 項目負(fù)責(zé)人聯(lián)系,請其將您添加為項目成員有滑。
- 按照 Firebase 集成指南操作跃闹,在您的應(yīng)用中實施 Firebase SDK。
- 完成集成后俺孙,請重新發(fā)布您的應(yīng)用辣卒,我們會在應(yīng)用概覽信息中心顯示相關(guān)的分析數(shù)據(jù)。
- 請查看 AdMob 政策睛榄,確保您的實現(xiàn)方案符合相關(guān)規(guī)定荣茫。
排錯
on failed for task ':childgit:app:processDebugGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it.
Searched Location:
F:\src\git_project\qq_qqrobot\childgit\app\src\nullnull\debug\google-services.json
F:\src\git_project\qq_qqrobot\childgit\app\src\debug\nullnull\google-services.json
F:\src\git_project\qq_qqrobot\childgit\app\src\null
All firebase libraries must be either above or below 14.0.0
fix
//https://dl.google.com/dl/android/maven2/index.html
implementation 'com.google.android.gms:play-services-ads:15.0.0'
// implementation 'com.google.android.gms:play-services-ads:12.0.1'
//implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.firebase:firebase-core:15.0.0'
進(jìn)入控制臺點擊firexx 然后點擊下載配置文件。放到這個目錄
https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest
最終
classpath 'com.google.gms:google-services:3.2.1' // google-services plugin
錯誤碼
我剛開始的時候是0
public final class AdRequest {
public static final int ERROR_CODE_INTERNAL_ERROR = 0;
public static final int ERROR_CODE_INVALID_REQUEST = 1;
public static final int ERROR_CODE_NETWORK_ERROR = 2;
public static final int ERROR_CODE_NO_FILL = 3;
https://github.com/googleads/googleads-mobile-android-examples
遇到問題可以提交到谷歌官方的論壇哈
https://groups.google.com/forum/#!forum/google-admob-ads-sdk
2018年05月07日
我現(xiàn)在官方的demo正常顯示场靴,強(qiáng)烈建議大家也下載demo玩啡莉,其實看文檔是很不全的港准。demo的演示比較全,我這里就只給大家做個導(dǎo)師了咧欣,哈哈浅缸,
我這里一直提示0,我以為是要等幾個小時魄咕,過了幾天打開官方網(wǎng)站看了一下衩椒,有一個警告信息。
提供詳細(xì)付款信息
只有在您提供此信息后 AdMob 才能投放您的廣告
這是比較棘手的哮兰,需要信用卡毛萌,而且我上次搞谷歌支付用招商這種雙幣信用卡都不行,雖然可以用來買0.99美元的書喝滞,虧了我不少錢阁将,后面還是不行,最后不得已去淘寶購買了禮品卡才搞定了右遭,然后用來購買開發(fā)者賬號
不過這次很幸運做盅,成功了,ok窘哈,錯誤碼從內(nèi)部錯誤0 變成沒有廣告的錯誤碼3了吹榴。
pulic static final int ERROR_CODE_INTERNAL_ERROR = 0;
public static final int ERROR_CODE_INVALID_REQUEST = 1;
public static final int ERROR_CODE_NETWORK_ERROR = 2;
public static final int ERROR_CODE_NO_FILL = 3;