问题描述
问题是当adReward完成时,我试图将用户名传递给另一个活动。但是,即使视频广告已加载,我仍会参与此活动。 (并且在没有视频广告展示时效果很好)。这是我的代码:
我的java.class
public class TheDay1 extends AppCompatActivity implements rewardedvideoadListener {
Vibrator vibrator;
private rewardedvideoad HDay1;
int currentActivity = 0;
boolean flag;
private EditText vname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_day1);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
MobileAds.initialize(this,"ca-app-pub-3940256099942544/6300978111");
///ADD BAN
AdView mAdView = findViewById(R.id.banner1);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
///////////////////////////////////
//VIDEO
HDay1 = MobileAds.getrewardedvideoadInstance(this);
HDay1.setrewardedvideoadListener(this);
loadRewardedVideoDAY1();
vname = findViewById(R.id.name);
final ImageView nm = findViewById(R.id.ID);
nm.setVisibility(View.INVISIBLE);
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
nm.setColorFilter(filter);
final Button closeAd = findViewById(R.id.closeDay1);
closeAd.setVisibility(View.INVISIBLE);
///////////////////////////////////////////////////////////////////////////////////////////
closeAd.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
vibrator.vibrate(50);
loadRewardedVideoDAY1();
if (HDay1.isLoaded()) {
HDay1.show();
setCurrent(1);
}
String name = vname.getText().toString();
getusername(name);
}
});
}
////////////////////////////////////////////////////////////////
private void Day1() {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
private void getusername(String name) {
Intent intent = new Intent(this,ActivityTwo.class);
Resources resources = getResources();
String key = resources.getString(R.string.key_name);
intent.putExtra(key,name);
startActivity(intent);
}
private void loadRewardedVideoDAY1() {
if (!HDay1.isLoaded()) {
HDay1 = null;
HDay1 = MobileAds.getrewardedvideoadInstance(this);
HDay1.setrewardedvideoadListener(this);
///////////////TEST ID//////////////////
HDay1.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
}
}
@Override
public void onrewardedvideoadLoaded() {
Log.d("LOADED!","onrewardedvideoadLoaded");
}
@Override
public void onrewardedvideoadOpened() {
Log.d("OPENED!","onrewardedvideoadLoaded");
}
@Override
public void onRewardedVideoStarted() {
Log.d("STARTED!","onrewardedvideoadLoaded");
}
@Override
public void onrewardedvideoadClosed() {
loadRewardedVideoDAY1();
if (currentActivity == 1) {
Day1();
}
}
@Override
public void onRewarded(RewardItem rewardItem) {
loadRewardedVideoDAY1();
flag = true;
if (currentActivity == 1) {
Day1();
}
}
@Override
public void onrewardedvideoadLeftApplication() {
}
@Override
public void onrewardedvideoadFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
}
@Override
protected void onPause() {
HDay1.pause(this);
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
public void setCurrent(int val){
currentActivity = val;
}
任何想法可能是什么问题?预先感谢
解决方法
尝试以下代码。使用您的意图逻辑更新ProcessData并使用课程中所需的其他数据更新oncreate
public class TheDay1 extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
private Boolean bRewardVideo = false;
@Override
public void onResume() {
super.onResume();
if (mRewardedVideoAd != null) {
mRewardedVideoAd.resume(this);
}
}
@Override
public void onPause() {
super.onPause();
if (mRewardedVideoAd != null) {
mRewardedVideoAd.pause(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mRewardedVideoAd != null) {
mRewardedVideoAd.destroy(this);
}
}
@Override
public void onRewardedVideoCompleted() {
//Toast.makeText(this,"onRewardedVideoCompleted",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 onRewardedVideoAdClosed() {
//Toast.makeText(this,"Reward Video Closed",Toast.LENGTH_SHORT).show();
// Load the next rewarded video ad.
if (bRewardVideo) {
ProcessData();
} else {
Toast.makeText(this,"Please View Reward Video to get Reward Points",Toast.LENGTH_LONG).show();
}
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(this,"Now you can close Ad to Process",Toast.LENGTH_LONG).show();
bRewardVideo = true;
}
@Override
public void onRewardedVideoAdLeftApplication() {
//Toast.makeText(this,"onRewardedVideoAdLeftApplication",Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
Toast.makeText(this,"Fail to Load Reward Video. Please try again!",Toast.LENGTH_SHORT).show();
}
private void loadRewardedVideoAd() {
bRewardVideo = false;
mRewardedVideoAd.loadAd(BuildConfig.REWARDVIDEOID,new AdRequest.Builder().build());
}
private void ProcessData() {
Snackbar.make(findViewById(R.id.drawer_layout_law),"Please wait while we process the request... ",Snackbar.LENGTH_INDEFINITE).show();
//WRITE YOUR INTENT CODE HERE
Intent intent = new Intent(this,ActivityTwo.class);
Resources resources = getResources();
String key = resources.getString(R.string.key_name);
intent.putExtra(key,name);
startActivity(intent);
}
private void ShowRewardVideoDialog() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
} else {
loadRewardedVideoAd();
Toast.makeText(mctx,"Fail to Load Reward Video : Please try again",Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
}