应用前提条件
- 使用 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-data
android: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() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
adRequest = new AdRequest.Builder().build();
loadJiliAd();
//ad必须在主线程上,使用消息通知调用
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
// 显示广告
showJiliad();
break;
case 1:
//测试加载js
webview.loadUrl("javascript:test()");
break;
default:
break;
}
}
};
//加载页面
showWeb();
}
private void showJiliad(){
if (mRewardedAd != null) {
Activity activityContext = this;
mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public 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(){
@Override
public 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 ;
@Override
public void onPageFinished(WebView view, String url)
{
//结束
super.onPageFinished(view, url);
Log.d("TAG", "加载结束");
if(r == 10){
showAD();
}
}
@Override
public 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() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d("TAG", loadAdError.toString());
mRewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
mRewardedAd = rewardedAd;
Log.d("TAG", "Ad was loaded.");
mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// 在为广告记录点击时调用。
Log.d("TAG", "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// 当广告被关闭时调用.
// 将广告引用设置为 null,这样您就不会再次展示广告。
Log.d("TAG", "Ad dismissed fullscreen content.");
mRewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// 广告无法展示时调用
Log.e("TAG", "Ad failed to show fullscreen content.");
mRewardedAd = null;
}
@Override
public void onAdImpression() {
// 在为广告记录展示时调用
Log.d("TAG", "Ad recorded an impression.");
}
@Override
public 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的函数
*/
@JavascriptInterface
public void startFunction() {
Toast.makeText(mContext,"我是安卓原生的弹窗",Toast.LENGTH_SHORT).show();
//调用激励视频广告
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
/**
* js调用Android的函数并给Android原生传递一个参数
*/
@JavascriptInterface
public void startFunction(String arg) {
Toast.makeText(mContext,arg,Toast.LENGTH_SHORT).show();
}
}
7,html调用激励奖励
<input type="button" value="点击调用android代码,拉取激励视频"
onclick="window.Android.startFunction()"/>
整体流程结束,业务代码可按需自行添加
|