Skip to content

Commit 7ee4899

Browse files
committed
Toast need run on UI thread
1 parent c5b4e62 commit 7ee4899

2 files changed

Lines changed: 30 additions & 19 deletions

File tree

app/src/main/java/com/huawei/hms/ads/sdk/InterstitialActivity.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,22 @@ public void onClick(View v) {
6363
@Override
6464
public void onAdLoaded() {
6565
super.onAdLoaded();
66-
Toast.makeText(InterstitialActivity.this, "Ad loaded", Toast.LENGTH_SHORT).show();
66+
showToast("Ad loaded");
67+
6768
// Display an interstitial ad.
6869
showInterstitial();
6970
}
7071

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

7878
@Override
7979
public void onAdClosed() {
8080
super.onAdClosed();
81-
Toast.makeText(InterstitialActivity.this, "Ad closed", Toast.LENGTH_SHORT).show();
81+
showToast("Ad closed");
8282
Log.d(TAG, "onAdClosed");
8383
}
8484

@@ -117,7 +117,16 @@ private void showInterstitial() {
117117
if (interstitialAd != null && interstitialAd.isLoaded()) {
118118
interstitialAd.show(this);
119119
} else {
120-
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
120+
showToast("Ad did not load");
121121
}
122122
}
123+
124+
private void showToast(final String text) {
125+
runOnUiThread(new Runnable() {
126+
@Override
127+
public void run() {
128+
Toast.makeText(InterstitialActivity.this, text, Toast.LENGTH_SHORT).show();
129+
}
130+
});
131+
}
123132
}

app/src/main/java/com/huawei/hms/ads/sdk/RewardActivity.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,12 @@ private void loadRewardAd() {
8787
RewardAdLoadListener rewardAdLoadListener = new RewardAdLoadListener() {
8888
@Override
8989
public void onRewardAdFailedToLoad(int errorCode) {
90-
Toast
91-
.makeText(RewardActivity.this, "onRewardAdFailedToLoad " + "errorCode is :" + errorCode,
92-
Toast.LENGTH_SHORT)
93-
.show();
90+
showToast("onRewardAdFailedToLoad " + "errorCode is :" + errorCode);
9491
}
9592

9693
@Override
9794
public void onRewardedLoaded() {
98-
Toast.makeText(RewardActivity.this, "onRewardedLoaded", Toast.LENGTH_SHORT).show();
95+
showToast("onRewardedLoaded");
9996
}
10097
};
10198

@@ -110,20 +107,19 @@ private void rewardAdShow() {
110107
rewardedAd.show(RewardActivity.this, new RewardAdStatusListener() {
111108
@Override
112109
public void onRewardAdClosed() {
110+
showToast("onRewardAdClosed");
111+
113112
loadRewardAd();
114113
}
115114

116115
@Override
117116
public void onRewardAdFailedToShow(int errorCode) {
118-
Toast
119-
.makeText(RewardActivity.this, "onRewardAdFailedToShow " + "errorCode is :" + errorCode,
120-
Toast.LENGTH_SHORT)
121-
.show();
117+
showToast("onRewardAdFailedToShow " + "errorCode is :" + errorCode);
122118
}
123119

124120
@Override
125121
public void onRewardAdOpened() {
126-
Toast.makeText(RewardActivity.this, "onRewardAdOpened", Toast.LENGTH_SHORT).show();
122+
showToast("onRewardAdOpened");
127123
}
128124

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

140+
private void showToast(final String text) {
141+
runOnUiThread(new Runnable() {
142+
@Override
143+
public void run() {
144+
Toast.makeText(RewardActivity.this, text, Toast.LENGTH_SHORT).show();
145+
}
146+
});
147+
}
148+
147149
/**
148150
* Set a score.
149151
*

0 commit comments

Comments
 (0)