qb02@アプリ屋さんの備忘録

アプリ作成時のメモを記載してます('Θ')

【Android】How to implement video reward advertisement

Android】 Remembrance on how to implement Admob video reward advertisement

【Windows10】

【Andoroid Studio 3.1.1】

 

The necessary information is below.

RewardedVideoAd mRewardedVideoAd;
String movie_id = "ca-app-pub-3940256099942544/5224354917"; // Admob Test ID
MobileAds.initialize(this, movie_id); // Initialize
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener;
mRewardedVideoAd.loadAd(movie_id, new AdRequest.Builder().build()); // ads request
mRewardedVideoAd.show(); // ads play
If Uncle Admob comes out, it can be implemented normally.

f:id:qb02:20180611113257p:plain

 

Implementation image

public class MainActivity extends AppCompatActivity {

RewardedVideoAd mRewardedVideoAd;
String movie_id = "ca-app-pub-3940256099942544/5224354917";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);

MobileAds.initialize(this, movie_id); // initialize
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);


mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener()
{
@Override
public void onRewarded(RewardItem reward) {
// Process when giving reward for video advertisement reproduction
}

@Override
public void onRewardedVideoAdLeftApplication() {
// Processing when leaving video advertisement (go to a browser, etc.)
}

@Override
public void onRewardedVideoAdClosed() {
// Processing at the end of playback of movie
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
// Processing when video loading fails
}

@Override
public void onRewardedVideoAdLoaded() {
// Processing when preparation for reproducing movie is completed

if (mRewardedVideoAd.isLoaded())
{
mRewardedVideoAd.show();
}
}

@Override
public void onRewardedVideoAdOpened() {
// Processing when transitioning to movie screen
}

@Override
public void onRewardedVideoStarted() {
// Processing when starting playback of movie
}

@Override
public void onRewardedVideoCompleted() {
// Processing when the viewer finishes watching a movie
}
});

mRewardedVideoAd.loadAd(movie_id, new AdRequest.Builder().build()); // ads request
}
}

 

import can be done with Alt + Enter

 

When publishing to Google Play, please change movie_id
It is OK if you change to ID for video reward advertisement acquired with Admob.

movie_id = "ca-app-pub-3940256099942544/5224354917"

movie_id = "ca-app-pub-xxxxxxxxxxxxxxxx/yyyyyyyyyy"

 

In the case of the above implementation, immediately after activating MainActivity, video advertisement flows suddenly. It seems that it is common to listen in the dialog etc etc whether to reproduce before the movie is played so that the user will not be surprised.

play.google.com

Actual implementation image

f:id:qb02:20180611131805p:plain
f:id:qb02:20180611131843p:plain

 

Hide ads to display a dialog

 

public class MainActivity extends AppCompatActivity {

RewardedVideoAd mRewardedVideoAd;
String movie_id = "ca-app-pub-3940256099942544/5224354917";
Button button_movie_Ad_delete;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);

button_movie_Ad_delete = (Button)findViewById(R.id.button_movie_Ad_delete);

MobileAds.initialize(this, movie_id); // 初期化
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); // インスタンスの生成


mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener()
{
@Override
public void onRewarded(RewardItem reward) {
// 動画広告再生に対する報酬を与えたときの処理
}

@Override
public void onRewardedVideoAdLeftApplication() {
// 動画広告から離れるときの処理(ブラウザに行くなど)
}

@Override
public void onRewardedVideoAdClosed() {
// 動画の再生を終了したときの処理
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
// 動画の読み込みが失敗したときの処理
}

@Override
public void onRewardedVideoAdLoaded() {
// 動画を再生する準備が完了したときの処理
if (mRewardedVideoAd.isLoaded())
{
mRewardedVideoAd.show();
}
}

@Override
public void onRewardedVideoAdOpened() {
// 動画画面に遷移したときの処理
}

@Override
public void onRewardedVideoStarted() {
// 動画を再生を開始したときの処理
}

@Override
public void onRewardedVideoCompleted() {
// 動画を最後まで見終わったときの処理
// 広告非表示のコードは省略
Toast.makeText(this, "ご視聴ありがとうございました!\n広告が非表示になります。", Toast.LENGTH_LONG).show();
}
});


button_movie_Ad_delete.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {

// 広告非表示ボタンが押されたときにダイアログを表示する
// AlertDlg.classのコードは省略。自分の好きな処理を実装すればOK
Intent intent = new Intent(getApplication(), AlertDlg.class);
startActivityForResult(intent, 0);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

// ダイアログでユーザーが選択した結果に応じて処理を変える
// 再生するを押下した場合は動画を再生、キャンセルの場合は何もしない
if(globals.Ad_delete_flag == true)
{
movie_view();
}
}

public void movie_view()
{
mRewardedVideoAd.loadAd(movie_id, new AdRequest.Builder().build()); // 広告のリクエス
Toast.makeText(this, "動画の再生準備をしています。\nしばらくお待ちください。", Toast.LENGTH_LONG).show();
}
}

 

that's all. 
play.google.com