I started to get interested in Google mobile ads. Right now I am testing RewardedVideoAd functionality with Google given testing ad. Everything works fine, but I can't find the way how to disable ad closing while the ad is not finished.
Below is my abstract code which shows how I am using the RewardedVideoAd method:
public class TestPage extends AppCompatActivity implements RewardedVideoAdListener {
private Button button;
private final static String APP_ADMOB_ID = "[my_app_admob_id]";
private RewardedVideoAd mRewardedVideoAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_page);
MobileAds.initialize(this, APP_ADMOB_ID);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "Video ad loaded",
Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "Video ad opened",
Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "Video ad started",
Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
#Override
public void onRewarded(RewardItem rewardItem) {
Toast.makeText(this, "onRewarded! currency: " + rewardItem.getType() + " amount: " +
rewardItem.getAmount(), Toast.LENGTH_LONG).show();
}
#Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "Left app during video ad",
Toast.LENGTH_LONG).show();
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
Toast.makeText(this, "Failed to load video ad", Toast.LENGTH_LONG).show();
}
}
Now, when ad video is displayed user can press back button and then appears dialog box with message: Close video?. I want to disable this ability.
But I don't know how it could be done. I tried to #Override onBackPressed() method and inserted if statement with checks boolean value if it is able to do something or not. But it does not work.
So my main question is:
Is there any way to disable ad closing while video is running?
Thank you for any type of help.
P.S.: I have read all "AdMob for Android" instructions and I don't need link of it.
Related
I want to detect screenshots inside a specific activity inside my app. I decided to use this ScreenShot Detector library but unfortunately this does not detect any screenshots for some reason.
I added some code to my activity to check if all the required permissions are grandent and it still doesn't work.
My Activity:
public class UserProfileActivity extends AppCompatActivity implements ScreenshotDetectionDelegate.ScreenshotDetectionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
ActivityResultLauncher<String> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
Toast.makeText(this, "Permission ok", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission req", Toast.LENGTH_SHORT).show();
// Explain to the user that the feature is unavailable because the
// features requires a permission that the user has denied. At the
// same time, respect the user's decision. Don't link to system
// settings in an effort to convince the user to change their
// decision.
}
});
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
// You can use the API that requires the permission.
requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
CustomIntent.customType(UserProfileActivity.this, "right-to-left");
}
#Override
protected void onStart() {
super.onStart();
screenshotDetectionDelegate.startScreenshotDetection();
}
#Override
protected void onStop() {
super.onStop();
screenshotDetectionDelegate.stopScreenshotDetection();
}
#Override
protected void onResume() {
super.onResume();
screenshotDetectionDelegate.startScreenshotDetection();
}
#Override
public void onScreenCaptured(#NonNull String s) {
sendNotificationToUser("screenshot");
Toast.makeText(UserProfileActivity.this, "Profile Screenshotted", Toast.LENGTH_SHORT).show();
}
#Override
public void onScreenCapturedWithDeniedPermission() {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
I'm trying to integrate the interstitial ad from Huawei Ads, when I click on (X) or back button to close the ad, for some reason the Toast inside onAdClosed method is not showing.
huaweiInterstitialAd = new InterstitialAd(MainActivity.this);
huaweiInterstitialAd.setAdId("teste9ih9j0rc3");
private void showHuaweiInterstitial() {
if (huaweiInterstitialAd != null && huaweiInterstitialAd.isLoaded()) {
huaweiInterstitialAd.show(MainActivity.this);
}
}
public void loadHuaweiInterstitialAd() {
huaweiInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
showHuaweiInterstitial();
}
#Override
public void onAdFailed(int errorCode) {
Log.d(TAG, "Ad load failed with error code: " + errorCode);
}
#Override
public void onAdLeave() {
super.onAdLeave();
Log.d(TAG, "onAdLeave");
}
#Override
public void onAdClosed() {
super.onAdClosed();
Toast.makeText(MainActivity.this, "Ad Closed", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdClicked() {
Log.d(TAG, "onAdClicked");
super.onAdClicked();
}
#Override
public void onAdOpened() {
Log.d(TAG, "onAdOpened");
super.onAdOpened();
}
});
AdParam adParam = new AdParam.Builder().build();
huaweiInterstitialAd.loadAd(adParam);
}
The problem only occurs if you use:
implementation 'com.huawei.hms:ads:3.4.46.300'
But everything works fine with :
implementation 'com.huawei.hms:ads-lite:13.4.46.300'
I am getting this error when using InterstitialAdListener
Anonymous class derived from InterstitialAdListener' must either be declared > abstract or implement abstract method 'onInterstitialDismissed(Ad) in
'InterstitialAdListener'
The InterstitialAdListener is red color underlined.
I tried reduced it to com.facebook.ads.AdListener but doesn't work with me Also, I upgrade SDK to see if it would solve it but didn't have luck.
How I can solve this problem ?
private void initInterstitialAdPrepare() {
interstitialAd = new InterstitialAd(this, "xxxxxxxx_xxxxxxxxx");
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onInterstitialDisplayed(Ad ad) {
// Interstitial ad displayed callback
Log.e(TAG, "Interstitial ad displayed.");
}
#Override
public void onError(Ad ad, AdError adError) {
// Ad error callback
Log.e(TAG, "Interstitial ad failed to load: " + adError.getErrorMessage());
}
#Override
public void onAdLoaded(Ad ad) {
// Interstitial ad is loaded and ready to be displayed
Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
}
// Show the ad
interstitialAd.show();
}
Call this in onCreate
private void requestInterstitialAd(){
interstitialAd = new InterstitialAd(this, getResources().getString(R.string.interstitial_ad_id)); //YOUR_PLACEMENT_ID
interstitialAd.loadAd();
}
Use this code where you want to load ad and for splash use it once your splash timer is completed
if(interstitialAd.isAdLoaded()) {
interstitialAd.show();
interstitialAd.setAdListener(new InterstitialAdListener() {
#Override
public void onError(Ad ad, AdError adError) {
}
#Override
public void onInterstitialDismissed(Ad ad) {
requestInterstitialAd();
//Perform your functionality here
}
#Override
public void onInterstitialDisplayed(Ad ad) {
}
#Override
public void onAdLoaded(Ad ad) {
}
#Override
public void onAdClicked(Ad ad) {
}
#Override
public void onLoggingImpression(Ad ad) {
}
});
}
else {
requestInterstitialAd();
//Perform your functionality here in case ad is not loaded before
}
I want to use Rewarded video ads (Admob) but I want to show a progress bar while the video ads is loading
I already try to did it with async task just to see if the video will load but it didn't work
#SuppressLint("StaticFieldLeak")
public class videoAd extends AsyncTask<Void, Void, Void> {
#Override
protected void doInBackground(Void... voids) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
});
}
#Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if (mRewardedVideoAd.isLoaded()){
Toast.makeText(SetFullWallpaper.this, "Video loaded", Toast.LENGTH_SHORT).show();
mRewardedVideoAd.show();
}
}
}
Now I want to load a progress bar if the video is not loaded yet
Thank you
This is how I did it:
I had a button, which when clicked showed the ad, so I had a boolean variable which tracked whether the button has been clicked:
boolean buttonClicked = false
These lines were in my onCreate function:
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getContext());
rewardedVideoAdListener = new RewardedVideoAdListener() {
#Override
public void onRewardedVideoAdLoaded() {
if(buttonClicked) {
showAd();
}
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
if(buttonClicked) {
progressBar.setVisibility(View.INVISIBLE);
Toast toast = Toast.makeText(getContext(), "Please try again later", Toast.LENGTH_SHORT);
toast.show();
}
}
#Override
public void onRewardedVideoCompleted() {
}
};
mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener);
loadRewardedVideoAd();
pointsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showAd();
}
});
This was my showAd function:
public void showAd(){
if (mRewardedVideoAd.isLoaded()) {
progressBar.setVisibility(View.GONE);
mRewardedVideoAd.show();
buttonClicked = false;
} else {
loadRewardedVideoAd();
progressBar.setVisibility(View.VISIBLE);
buttonClicked = true;
}
}
How this works is, the app tries to load the ad in the background by calling the loadRewaredVideoAd() function when the activity/fragment is created. Then when the user clicks the button,showAd() function is called and one of two things happen:
1) If the ad was successfully loaded, it shows the ad.
2) If not, it calls loadRewardedVideoAd() again and shows a progressbar this time. It also sets buttonClicked to true. Then if the ad loads, the onRewardedVideoAdLoaded() function is called which calls showAd() again and this time the 1st option happens.
If the ad didn't load this time as well, then onRewardedVideoAdFailedToLoad(int i)is called and it shows a toast saying the user to try again later.
add OnProgressUpdate() in your class that extends AsyncTask and add progress dialog in this.
I am placing an admob video, which is executed when you enter the activity. In my case, I put an executable loop to start the video when it is already loaded.
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}else{
loadRewardedVideoAd();
}
This is part of all my code
private boolean started = false;
private Handler handler = new Handler();
private void initializeAdMob() {
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
if (!mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.loadAd(getString(R.string.id_block_activity), new AdRequest.Builder().build());
}
}
private void showRewardedVideoAd() {
if (BaseInteractor.isNetworkAvailable(this)) {
showProgressBar(true);
start();
}
}
private Runnable runnable = new Runnable() {
#Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}else{
loadRewardedVideoAd();
}
if (started) {
start();
}
}
};
public void stop() {
started = false;
handler.removeCallbacks(runnable);
}
public void start() {
started = true;
handler.postDelayed(runnable, 2000);
}
finally I stop the runnable in
#Override
public void onRewardedVideoAdOpened() {
showProgressBar(false);
stop();
}
I hope this helps you.
Does anybody have an idea about this website?
I am trying the code provided here, the status uploading looks fine but i can't upload any image seems as if UploadImageAsyn() is not working. Here's my code
public class ShareButtonActivity extends Activity {
// SocialAuth Component
SocialAuthAdapter adapter;
Bitmap bitmap;
// Android Components
Button update;
EditText edit;
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img =(ImageView) findViewById(R.id.imageView1);
img.buildDrawingCache();
bitmap = img.getDrawingCache();
// Create Your Own Share Button
Button share = (Button) findViewById(R.id.sharebutton);
share.setText("Share");
// Add it to Library
adapter = new SocialAuthAdapter(new ResponseListener());
// Add providers
adapter.addProvider(Provider.FACEBOOK, R.drawable.facebook);
// Enable Provider
adapter.enable(share);
}
/**
* Listens Response from Library
*
*/
private final class ResponseListener implements DialogListener {
#Override
public void onComplete(Bundle values) {
// Get name of provider after authentication
final String providerName=values.getString(SocialAuthAdapter.PROVIDER);
Log.d("ShareButton", "Provider Name = " + providerName);
Toast.makeText(ShareButtonActivity.this, providerName + " connected", Toast.LENGTH_LONG).show();
update = (Button) findViewById(R.id.update);
update.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
adapter.uploadImageAsync("Landscape Images", "icon.png", bitmap, 0,new UploadImageListener());
Toast.makeText(getApplicationContext(),"yoohooo ",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_SHORT).show();
}
}
});
}
#Override
public void onError(SocialAuthError error) {
Log.d("ShareButton", "Authentication Error: " + error.getMessage());
}
#Override
public void onCancel() {
Log.d("ShareButton", "Authentication Cancelled");
}
#Override
public void onBack() {
Log.d("Share-Button", "Dialog Closed by pressing Back Key");
}
}
// To get status of message after authentication
private final class UploadImageListener implements SocialAuthListener<Integer> {
#Override
public void onExecute(String provider, Integer t) {
Log.d("Custom-UI", "Uploading Data");
Integer status = t;
Log.d("Custom-UI", String.valueOf(status));
Toast.makeText(getApplicationContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(SocialAuthError arg0) {
// TODO Auto-generated method stub
}
}
}
Every time i click the update button a "no" message is printed on toast. Although the program does not show any error. I would be really greatfull if anybody can help me out on this.