There is an Android mechanism that allows you to run Android function from javascript:
<input class="button" type="button" value="FREE life" onclick="startRewardVideo('some parameters can be passed to Android from here')">
<script type="text/javascript">
function startRewardVideo(paramFromJS) {
Android.startRewardVideoAndroidFunction(paramFromJS);
}
</script>
now you need class that knows what to do with your javascript:
package
fr.bbbb.myapplication;
import
android.content.Context;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.view.View;
import
android.webkit.JavascriptInterface;
import
android.webkit.WebChromeClient;
import
android.webkit.WebView;
import
android.webkit.WebViewClient;
import
android.webkit.WebSettings;
import
android.widget.Toast;
import
com.google.android.gms.ads.AdRequest;
import
com.google.android.gms.ads.MobileAds;
import
com.google.android.gms.ads.reward.RewardItem;
import
com.google.android.gms.ads.reward.RewardedVideoAd;
import
com.google.android.gms.ads.reward.RewardedVideoAdListener;
public
class
MainActivity
extends
AppCompatActivity
implements
RewardedVideoAdListener{
private
WebView webView;
private
RewardedVideoAd mRewardedVideoAd;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(
new
WebViewClient());
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(
true
);
webView.setWebChromeClient(
new
WebChromeClient());
MobileAds.initialize(
this
,
"ca-app-pub-3940256099942544~3347511713"
);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(
this
);
mRewardedVideoAd.setRewardedVideoAdListener(
this
);
webView.addJavascriptInterface(
new
MyJavascriptInterface(
this
) ,
"Android"
);
mRewardedVideoAd.loadAd(
"
ca-app-pub-3940256099942544/5224354917"
,
new
AdRequest.Builder().build());
}
public
class
MyJavascriptInterface {
private
Context context;
public
MyJavascriptInterface(Context context){
this
.context = context;
}
@JavascriptInterface
public
void
startRewardVideoAndroidFunction(){
runOnUiThread(
new
Runnable() {
public
void
run() {
if
(mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
}
}
private
void
loadRewardedVideoAd() {
mRewardedVideoAd.loadAd(
"ca-app-pub-8182467776458634/7199599910"
,
new
AdRequest.Builder().build());
}
@Override
public
void
onRewardedVideoAdLeftApplication() {
Toast.makeText(
this
,
"onRewardedVideoAdLeftApplication"
,Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewardedVideoAdClosed() {
Toast.makeText(
this
,
"onRewardedVideoAdClosed"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewardedVideoAdFailedToLoad(
int
errorCode) {
Toast.makeText(
this
,
"onRewardedVideoAdFailedToLoad"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewardedVideoAdLoaded() {
Toast.makeText(
this
,
"onRewardedVideoAdLoaded"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewardedVideoAdOpened() {
Toast.makeText(
this
,
"onRewardedVideoAdOpened"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewardedVideoStarted() {
Toast.makeText(
this
,
"onRewardedVideoStarted"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewardedVideoCompleted() {
Toast.makeText(
this
,
"onRewardedVideoCompleted"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onRewarded(RewardItem rewardItem) {
loadRewardedVideoAd();
}
}
Here you have full tutorial:
https://stackoverflow.com/questions/48403912/webview-remote-site-and-reward-videos?rq=1
https://openclassrooms.com/forum/sujet/rewarded-video-sur-une-application-webview?page=1
No hay comentarios:
Publicar un comentario