Skip to content

Commit

Permalink
Toast need run on UI thread
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqiuwei committed Aug 5, 2021
1 parent c5b4e62 commit 7ee4899
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
19 changes: 14 additions & 5 deletions app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ public void onClick(View v) {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Toast.makeText(InterstitialActivity.this, "Ad loaded", Toast.LENGTH_SHORT).show();
showToast("Ad loaded");

// Display an interstitial ad.
showInterstitial();
}

@Override
public void onAdFailed(int errorCode) {
Toast.makeText(InterstitialActivity.this, "Ad load failed with error code: " + errorCode,
Toast.LENGTH_SHORT).show();
showToast("Ad load failed with error code: " + errorCode);
Log.d(TAG, "Ad load failed with error code: " + errorCode);
}

@Override
public void onAdClosed() {
super.onAdClosed();
Toast.makeText(InterstitialActivity.this, "Ad closed", Toast.LENGTH_SHORT).show();
showToast("Ad closed");
Log.d(TAG, "onAdClosed");
}

Expand Down Expand Up @@ -117,7 +117,16 @@ private void showInterstitial() {
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show(this);
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
showToast("Ad did not load");
}
}

private void showToast(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(InterstitialActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
30 changes: 16 additions & 14 deletions app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,12 @@ private void loadRewardAd() {
RewardAdLoadListener rewardAdLoadListener = new RewardAdLoadListener() {
@Override
public void onRewardAdFailedToLoad(int errorCode) {
Toast
.makeText(RewardActivity.this, "onRewardAdFailedToLoad " + "errorCode is :" + errorCode,
Toast.LENGTH_SHORT)
.show();
showToast("onRewardAdFailedToLoad " + "errorCode is :" + errorCode);
}

@Override
public void onRewardedLoaded() {
Toast.makeText(RewardActivity.this, "onRewardedLoaded", Toast.LENGTH_SHORT).show();
showToast("onRewardedLoaded");
}
};

Expand All @@ -110,20 +107,19 @@ private void rewardAdShow() {
rewardedAd.show(RewardActivity.this, new RewardAdStatusListener() {
@Override
public void onRewardAdClosed() {
showToast("onRewardAdClosed");

loadRewardAd();
}

@Override
public void onRewardAdFailedToShow(int errorCode) {
Toast
.makeText(RewardActivity.this, "onRewardAdFailedToShow " + "errorCode is :" + errorCode,
Toast.LENGTH_SHORT)
.show();
showToast("onRewardAdFailedToShow " + "errorCode is :" + errorCode);
}

@Override
public void onRewardAdOpened() {
Toast.makeText(RewardActivity.this, "onRewardAdOpened", Toast.LENGTH_SHORT).show();
showToast("onRewardAdOpened");
}

@Override
Expand All @@ -132,10 +128,7 @@ public void onRewarded(Reward reward) {
// takes effect on the server. If no reward information is configured, grant a reward based on the
// actual scenario.
int addScore = reward.getAmount() == 0 ? defaultScore : reward.getAmount();
Toast
.makeText(RewardActivity.this, "Watch video show finished , add " + addScore + " scores",
Toast.LENGTH_SHORT)
.show();
showToast("Watch video show finished , add " + addScore + " scores");
score += addScore;
setScore(score);
loadRewardAd();
Expand All @@ -144,6 +137,15 @@ public void onRewarded(Reward reward) {
}
}

private void showToast(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(RewardActivity.this, text, Toast.LENGTH_SHORT).show();
}
});
}

/**
* Set a score.
*
Expand Down

0 comments on commit 7ee4899

Please sign in to comment.