Thuật ngữ quảng cáo trên di động có lẽ không còn xa lạ gì với các lập trình viên di động hiện nay. Admob là 1 trong những dịch vụ hỗ trợ lập trình viên dễ dàng hơn trong việc hiển thị và thu lợi nhuận từ quảng cáo. Trong bài viết này sẽ hướng dẫn cách hiển thị quảng cáo từ Admob khi lập trình game trên Android sử dụng Cocos2d-x.
Hiện thực
Thêm đoạn mã nguồn sau vào phương thức onCreate
trong Activity được Cocos2d-x tạo ra.
adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(AD_UNIT_ID); mContext = this;
Đoạn mã nguồn này để khởi tạo view cho quảng cáo cần hiển thị. Trong ví dụ này, khởi tạo kiểu của quảng cáo là BANNER
.
adView.setAdListener(new AdListener() { public void onAdLoaded() { if (mFirstTime) { mFirstTime = false; if (mWm == null) mWm = (WindowManager) mContext.getSystemService("window"); WindowManager.LayoutParams mLayoutPars = new WindowManager.LayoutParams(); mLayoutPars.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; mLayoutPars.width = WindowManager.LayoutParams.WRAP_CONTENT; mLayoutPars.height = WindowManager.LayoutParams.WRAP_CONTENT; mLayoutPars.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; mLayoutPars.gravity = Gravity.TOP; mWm.addView(adView, mLayoutPars); } }});
Vì muốn hiển thị quảng cáo liên tục trong game nên sẽ hiển thị view của quảng cáo khi game nhận được quảng cáo lần đầu tiên. Và tất nhiên quảng cáo sẽ được hiển thị liên tục trong game.
Để hiển thị quảng cáo ở phía trên màn hình game, thiết đặt thuộc tính gravity
của LayoutParams
như sau:
mLayoutParams.gravity = Gravity.TOP;
Cuối cùng, gửi yêu cầu để Admob gửi quảng cáo cho game:
AdRequest adRequest = new AdRequest.Builder() // .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE") .build(); adView.loadAd(adRequest);
Để game chỉ hiển thị các quảng cáo test (test ads) trên các thiết bị, cần phải bỏ comment dòng addTestDevice
với tham số là Hashed Device ID
được in ra trong log của Android.
Google Play Service
Để game có thể hiển thị được quảng cáo sử dụng Admob, phải sử dụng Google Play Service. Các bước thêm Google Play Service vào game như sau:
- Sao chép thư viện Google Play Service vào thư mục libs ở cùng vị trí với file AndroidManifest.
- Cấu hình file project.properties để game sử dụng Google Play Service. Nội dung file project.properties sau khi cấu hình có dạng như sau:
target=android-13 android.library.reference.1=../../../cocos2dx/platform/android/java android.library.reference.2=./libs/google-play-services_lib
AndroidManifest file
Thêm phần khai báo phiên bản Google Play Service và AdActivity vào file AndroidManifest để game có thể nhận được quảng cáo thành công. Sau khi thêm, file manifes sẽ có dạng như sau:
<application android:label="@string/app_name" android:icon="@drawable/icon"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation |screenLayout|uiMode|screenSize|smallestScreenSize"/> </application>
Build game
Để tiện cho việc tự động hoá, build game sử dụng command line. Dòng lệnh để build debug như sau:
./build_native.sh && ant debug