admob rewarded video ads android example
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".after32.Rewardvideoads"> <TextView android:id="@+id/text" android:gravity="center" android:layout_marginTop="50dp" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello_world" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/watch" android:text="show" android:layout_marginTop="50dp" android:layout_below="@+id/text" /> </RelativeLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; 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; import com.tutorial.personal.androidstudiopro.R; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Rewardvideoads extends AppCompatActivity implements RewardedVideoAdListener { private Button mWatch; private TextView mText; private RewardedVideoAd mAd; int coin=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rewardvideoads); mWatch=(Button)findViewById(R.id.watch); mText=(TextView) findViewById(R.id.text); mWatch.setVisibility(View.INVISIBLE); mAd= MobileAds.getRewardedVideoAdInstance(this); mAd.setRewardedVideoAdListener(this); ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); scheduler.scheduleAtFixedRate(new Runnable() { public void run() { runOnUiThread(new Runnable() { public void run() { if (!mAd.isLoaded()) { loadAd(); } else { } } }); } }, 3, 5, TimeUnit.SECONDS); mWatch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(mAd.isLoaded()){ mAd.show(); } } }); } public void loadAd(){ if(!mAd.isLoaded()){ mAd.loadAd("ca-app-pub-394454099942544/55445917",new AdRequest.Builder().build()); } } @Override public void onRewardedVideoAdLoaded() { mWatch.setVisibility(View.VISIBLE); Toast.makeText(this, "Click this button for earn coin", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdOpened() { Toast.makeText(this, "Rewarded Video Ad has opened", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoStarted() { Toast.makeText(this, "Watch full video for earn Coin", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdClosed() { Toast.makeText(this, "Rewarded Video Ad has closed", Toast.LENGTH_SHORT).show(); loadAd(); mWatch.setVisibility(View.INVISIBLE); } @Override public void onRewarded(RewardItem rewardItem) { coin=coin+rewardItem.getAmount(); mText.setText("Your coin : "+coin); } @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(this, "Rewarded Video Ad has closed", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdFailedToLoad(int i) { } @Override public void onRewardedVideoCompleted() { } @Override public void onPause() { super.onPause(); mAd.pause(this); } @Override public void onResume() { super.onResume(); mAd.resume(this); loadAd(); } @Override public void onDestroy() { super.onDestroy(); mAd.destroy(this); } @Override protected void onStart() { loadAd(); super.onStart(); } } |