I want to show admob or appodeal ads in service and app wont be shown in the recent apps.
I have tried many ways but didn't work.
I found this way but the app will come in recent apps!
public class ServiceAd extends Service {
private InterstitialAd mInterstitialAd;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
mInterstitialAd = new InterstitialAd(getApplicationContext());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
Log.d("tag", "on closed ad");
}
});
}
#Override
public int onStartCommand(final Intent intent, int flags, int startId) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("tag", "The interstitial wasn't loaded yet.");
}
}
}, 5000); // 5 mins
return START_STICKY;
}
}
Related
I want to set/add a delay when reloading an ad when it has failed to load. I think it could be done with Looper.getMainLooper() but I don´t know how to do this. You can see I have implemented that if the ad fails to load,it will be reloaded up to 5 times, so I think that adding a delay before reloading the ad could be a good option.
Here is my code:
#Override
protected void onResume() {
super.onResume();
requestNewInterstitial(5);
}
private void requestNewInterstitial(int maxRetry) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
if (maxRetry>0){
/*HERE IS WHERE THE AD IS RELOADED, SO THE DELAY COULD BE SET HERE BEFORE
CALLING THE REQUEST NEW INTERSTITIAL FUNCTION*/
mInterstitialAd = null;
requestNewInterstitial(maxRetry-1);
}
});
}
}
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
I tried to do this.
if (maxRetry>0){
//add a delay before retrying
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
requestNewInterstitial(maxRetry-1);
}
}, 1000);
}
Hope this fixes your problem.
You can use a simple Handler to delay a function like this:
(Kotlin version, but you get the idea)
private fun reloadInterstitialAd() {
if (maxRetry > 0) {
val delayTime = 5000L // 5 seconds.
val looper = mainLooper // Use `Looper.getMainLooper()` if you do not have access to an Activity context.
mInterstitialAd = null
Handler(looper).postDelayed({
maxRetry--
requestNewInterstitial(maxRetry)
}, delayTime)
} else {
Log.d(TAG, "Max reload reached")
}
}
Use this function like reloadInterstitialAd() in the onAdFailedToLoad callback.
You can use this
Thread.sleep(delay_time_in_milliseconds);
I'm using this code to load and show an interstitial ad. It works fine but as you can see if the ad fails to load onAdFailedToLoad, the interstitial ad is recursively requested. Maybe this could be a really bad practice as if the ad is not loaded, the function could be called a huge number of times. But, I have thought that as I'm requesting the ad after the device is connected to the internet, the probability that the ad is loaded should be almost 100%, but I was just wondering if it is a bad practice to do this. The interstitial ad is shown in the 2 activities, as you can see in one of them, it is shown when a button is clicked and after it is clicked there is an intent to move to another activity. The other activity that the interstitial is shown is almost the same, in this second activity it is shown when the game is paused and also when the level is finished. I don´t know if it is important but all the levels are in the same activity, so the interstitial as I have said is only shown in 2 different activities.
#Override
protected void onResume() {
super.onResume();
CallNewInertial();
}
private void CallNewInertial() {
ConnectionDetector cd = new ConnectionDetector(Activityone.this);
if (cd.isConnectingToInternet()) {
requestNewInterstitial();
}
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
requestNewInterstitial();
}
});
}
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
fixed code to make it perfect:
#Override
protected void onResume() {
super.onResume();
requestNewInterstitial(5);
}
private void requestNewInterstitial(int maxRetry) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
if (maxRetry>0){
mInterstitialAd = null;
requestNewInterstitial(maxRetry-1);
}
}
});
}
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
(using delay),updated code to make it even more perfect:
#Override
protected void onResume() {
super.onResume();
requestNewInterstitial(5);
}
private void requestNewInterstitial(int maxRetry) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
if (maxRetry>0){
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd = null;
requestNewInterstitial(maxRetry-1);
}
//delay of 10 seconds
}, 10000);
}
});
}
}
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
I have integrated Admob ads in my app and the I working fine, but I don´t know if I can omit the step of checking if the device is connected to the internet to request an interstitial ad.
This is my code:
//connectiondetec.java:
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
//noinspection deprecation
NetworkInfo[] info = connectivity.getAllNetworkInfo();
for (NetworkInfo anInfo : info)
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
}
//Activityone.java:
#Override
protected void onResume() {
super.onResume();
CallNewInertial();
}
private void CallNewInertial() {
ConnectionDetector cd = new ConnectionDetector(Activityone.this);
if (cd.isConnectingToInternet()) {
requestNewInterstitial();
}
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
requestNewInterstitial();
}
});
}
So, could I delete the connection detector class so that now the CallNewInertial is changed to the following code?
private void CallNewInertial() {
requestNewInterstitial();
}
updated code to make it work perfect:
#Override
protected void onResume() {
super.onResume();
requestNewInterstitial(5);
}
private void requestNewInterstitial(int maxRetry) {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(Activityone.this, getString(R.string.interid),
adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
if (maxRetry>0){
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd = null;
requestNewInterstitial(maxRetry-1);
}
}, 10000);
}
});
}
}
btnPause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd != null) {
mInterstitialAd.show(Activityone.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
}
#Override
public void onAdFailedToShowFullScreenContent(#NonNull AdError adError) {
}
});
It's not a common practice to check the internet connection before loading ad. It's also not very common to reload ad when it failed to load, because usually you start loading a new Interstitial anyway when entering next screen / loading next game level.
However, if your app does not have "natural" points when you request a new ad, then it's fine to reload it in onAdFailedToLoad, but limit the attempts to let's say 3 times and optionally add some delay before next attempt.
my app crashes while loading Google Admob ad. It worked fine for a long time but now I get following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.interstitial.InterstitialAd.setFullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback)' on a null object reference
I've changed nothing in code, my app is in Play Store and I have the App on my Galaxy S10 & it worked all the time but now if the Ad has to be loaded I get this error. Can somebody help?
Thanks
Edit: Here's my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-93706071examplenumber/4141725063", adRequest, new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
Log.i("TAG", "onAdLoaded");
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
Log.i("TAG", "onAdLoaded");
mInterstitialAd = null;
}
});
private void showInterstitial(){
if (callbackActive == false){
setCallback();
}
if (mInterstitialAd != null ) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
}
private void setCallback(){
callbackActive = true;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdDismissedFullScreenContent() {
Log.d("TAG", "The ad was dismissed.");
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
mInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
Put setFullScreenContentCallback in onAdLoaded
here is a code snippet (see code below) and check the full code from AdMob official sample on GitHub
public void loadAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
this,
AD_UNIT_ID,
adRequest,
new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
MyActivity.this.interstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
Toast.makeText(MyActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
interstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
#Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
// Make sure to set your reference to null so you don't
// show it a second time.
MyActivity.this.interstitialAd = null;
Log.d("TAG", "The ad was dismissed.");
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
// Make sure to set your reference to null so you don't
// show it a second time.
MyActivity.this.interstitialAd = null;
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d("TAG", "The ad was shown.");
}
});
}
Good day, what seems to be the problem. In my onAdClosed, i placed "ThirdActivity.class" , but when i close the Admob interstitial ad it goes to a different activity.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new com.google.android.gms.ads.AdListener() {
#Override
public void onAdClosed() {
Intent intent = new Intent(Dashboard.this, 3rdActivity.class);
startActivity(intent);
}
#Override
public void onAdLoaded() { Log.i("Ads", "onAdLoaded"); }
#Override
public void onAdFailedToLoad(int errorCode) { Log.i("Ads", "onAdFailedToLoad"); }
#Override
public void onAdOpened() { Log.i("Ads", "onAdOpened"); }
#Override
public void onAdLeftApplication() { Log.i("Ads", "onAdLeftApplication"); }
});
.
.
.
builder.setNeutralButton("Continue", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Intent intent = new Intent(Dashboard.this, ThirdActivity.class);
startActivity(intent);
}
}
});
AlertDialog coldialog = builder.create();
coldialog.show();