H5页面调用admob激励视频,用户获取奖励
生活随笔
收集整理的這篇文章主要介紹了
H5页面调用admob激励视频,用户获取奖励
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://developers.google.com/admob/android/rewarded?hl=zh-cn#set_the_fullscreencontentcallback
應用前提條件
- 使用 Android Studio 3.2 或更高版本
-
確保您應用的 build 文件使用以下值:
- minSdkVersion?為?16?或更高版本
- compileSdkVersion?為?28?或更高版本
1,在您的項目級?build.gradle?文件中,同時在?buildscript?和?allprojects?兩個部分中添加
buildscript {repositories {google()mavenCentral()} }allprojects {repositories {google()mavenCentral()} }2,將 Google 移動廣告 SDK 的依賴項添加到您的模塊的應用級?Gradle?文件,通常為?app/build.gradle:
dependencies {implementation 'com.google.android.gms:play-services-ads:21.0.0' }3,將您的 AdMob 應用 ID(可在 AdMob 界面中找到)添加到應用的?AndroidManifest.xml?文件中。為此,請添加包含?android:name="com.google.android.gms.ads.APPLICATION_ID"?的?<meta-data>?代碼。您可以在 AdMob 界面中找到您的應用 ID。對于?android:value,請插入您自己的 AdMob 應用 ID,并用英文引號括起來。
測試ID:ca-app-pub-3940256099942544~3347511713
注意:開發工程一定要使用測試ID,不要使用線上ID,否則可能導致線上admob廣告流量無效,最嚴重導致封號!
<manifest><application><!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 --><meta-dataandroid:name="com.google.android.gms.ads.APPLICATION_ID"android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/></application> </manifest>4,初始化廣告和預加載激勵視頻
import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.initialization.InitializationStatus; import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;public class MainActivity extends AppCompatActivity {private AdRequest adRequest;private Handler mHandler;private WebView webview;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);MobileAds.initialize(this, new OnInitializationCompleteListener() {@Overridepublic void onInitializationComplete(InitializationStatus initializationStatus) {}});adRequest = new AdRequest.Builder().build();loadJiliAd();//ad必須在主線程上,使用消息通知調用mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 0:// 顯示廣告showJiliad();break;case 1://測試加載jswebview.loadUrl("javascript:test()");break;default:break;}}};//加載頁面 showWeb();}private void showJiliad(){if (mRewardedAd != null) {Activity activityContext = this;mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {@Overridepublic void onUserEarnedReward(@NonNull RewardItem rewardItem) {// 用戶獲得獎勵Log.d("TAG", "The user earned the reward.");int rewardAmount = rewardItem.getAmount();String rewardType = rewardItem.getType();showJiangli("恭喜獲得獎勵");}});}else{showJiangli("廣告拉取中,請稍候再試");loadJiliAd();}}private void showJiangli(String msg){Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();}private void showWeb(){//實例化WebView對象webview = new WebView(this);//設置WebView屬性,能夠執行Javascript腳本WebSettings webSettings = webview.getSettings();webSettings.setJavaScriptEnabled(true);//加載頁面 webview.loadUrl("https://xxx.com/");//WebAppInterface 是我們自定義的一個類,通過實例化一個WebAppInterface 對象來和html進行綁定WebAppInterface webAppInterface = new WebAppInterface(this,mHandler);webview.addJavascriptInterface(webAppInterface, "Android");webview.setWebViewClient(new WebViewClient(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url){view.loadUrl(url);return true;}});webview.setWebViewClient(new WebViewClient(){Random ra =new Random();int r = ra.nextInt(20) + 1 ;@Overridepublic void onPageFinished(WebView view, String url){//結束super.onPageFinished(view, url);Log.d("TAG", "加載結束");if(r == 10){showAD();}}@Overridepublic void onPageStarted(WebView view, String url, Bitmap favicon){//開始super.onPageStarted(view, url, favicon);Log.d("TAG", "加載開始");num ++;if(r == 6){if (mInterstitialAd != null) {showAD();}}else if(num >= 6){if (mInterstitialAd != null) {num =0;showAD();}else{Log.d("TAG", "廣告未加載成功");}}Log.d("TAG", "加載NUM:" + String.valueOf(num));}});private void loadJiliAd(){RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",adRequest, new RewardedAdLoadCallback() {@Overridepublic void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {// Handle the error.Log.d("TAG", loadAdError.toString());mRewardedAd = null;}@Overridepublic void onAdLoaded(@NonNull RewardedAd rewardedAd) {mRewardedAd = rewardedAd;Log.d("TAG", "Ad was loaded.");mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {@Overridepublic void onAdClicked() {// 在為廣告記錄點擊時調用。Log.d("TAG", "Ad was clicked.");}@Overridepublic void onAdDismissedFullScreenContent() {// 當廣告被關閉時調用.// 將廣告引用設置為 null,這樣您就不會再次展示廣告。Log.d("TAG", "Ad dismissed fullscreen content.");mRewardedAd = null;}@Overridepublic void onAdFailedToShowFullScreenContent(AdError adError) {// 廣告無法展示時調用Log.e("TAG", "Ad failed to show fullscreen content.");mRewardedAd = null;}@Overridepublic void onAdImpression() {// 在為廣告記錄展示時調用Log.d("TAG", "Ad recorded an impression.");}@Overridepublic void onAdShowedFullScreenContent() {// 顯示廣告時調用.Log.d("TAG", "Ad showed fullscreen content.");}});}});} }6,webAppInterface類
public class WebAppInterface {Activity mContext;Handler mHandler;/*** Instantiate the interface and set the context*/WebAppInterface(Activity c,Handler r) {mContext = c;mHandler = r;}/*** js調用Android的函數*/@JavascriptInterfacepublic void startFunction() {Toast.makeText(mContext,"我是安卓原生的彈窗",Toast.LENGTH_SHORT).show();//調用激勵視頻廣告Message msg = new Message();msg.what = 0;mHandler.sendMessage(msg);}/*** js調用Android的函數并給Android原生傳遞一個參數*/@JavascriptInterfacepublic void startFunction(String arg) {Toast.makeText(mContext,arg,Toast.LENGTH_SHORT).show();} }7,html調用激勵獎勵
<input type="button" value="點擊調用android代碼,拉取激勵視頻" onclick="window.Android.startFunction()"/>整體流程結束,業務代碼可按需自行添加
總結
以上是生活随笔為你收集整理的H5页面调用admob激励视频,用户获取奖励的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 社交网络电影计算机分析,基于社交网络的电
- 下一篇: PCB多层板设计技巧