AdMob,after banner click,device back button not working - android

I have integrated admob in my android game of cocos2dx,but the problem i am facing is with back button.Once user click on bannar ads and try to press back button of device the back button method is not getting called.
The admob inegration is as below:-
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.build();
adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
adView.setBackgroundColor(0);
addContentView(adView,adParams);
_appActiviy = this;
ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
});
}
}, 0, 30, TimeUnit.SECONDS);
And the back button integration is as below through jni:-
_appActiviy.runOnUiThread(new Runnable() {
#Override
public void run() {
new AlertDialog.Builder(_appActiviy).setMessage("Are you sure you want to quit?").
setCancelable(true).setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Log.d("", "ShowRateUsAndQuitDialog ");
((Cocos2dxActivity)_appActiviy).finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}).setNegativeButton("No", null).show();
UntanglePro.showRateDialog(getContext(),null);
}
});

Related

Can't close or click on Admob Apps

I have been using Admob for a few months now, but since today a strange error is happening in my App. Everytimr an Interstitial Ad is showing up, I can't interact with it. Nothing happens when either when I click the X or on the Ad itself. So the App cannot be used after this ad showed up. I didn't change anything at all in my app. Has anybody ever had this issue before and may can tell me how to fix it? Thank you very much!
MainActivity:
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
internetIsConnected();
ShowConsentsDialog();
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-9428175386269844/3192729266");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
mInterstitialAd.show();
}
});
Handler handler0 = new Handler();
Handler handler = new Handler();
Handler handler1 = new Handler();
Handler handler2 = new Handler();
Handler handler3 = new Handler();
Handler handler4 = new Handler();
Handler handler5 = new Handler();
handler0.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 12000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 120000);
handler1.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 220000);
handler2.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 380000);
handler3.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 510000);
handler4.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 625000);
handler5.postDelayed(new Runnable() {
#Override
public void run() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}, 823000);
}

build AdMob Interstitial ad again after 60 seconds

Currently, I am using the code which is displaying AdMob Interstitial ad after 20 seconds and when the ad is closed I want to show another ad after 60 seconds, this is the code currently I am using for the first ad to load after 20 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
interAd = new InterstitialAd(MainActivity.this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
interAd.show();
}
});
interAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
}
});
}
} , 20000);
i have tried this method from AdMob tutorial it load the ads but after every second
interAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
interAd.loadAd(new AdRequest.Builder().build());
}
});
You should attach you Ad's lifecycle to your Activity's lifecycle so you can start/stop showing adds just when your app is visible to the user.
Create a global variables for you Ad and Handler
private InterstitialAd mInterstitialAd;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
// Wait 60 seconds
mHandler.postDelayed(this, 60*1000);
// Show Ad
showInterstitial();
}
};
then...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the InterstitialAd and set the adUnitId
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
#Override
protected void onStart() {
super.onStart();
// Start showing Ad in every 60 seconds
//when activity is visible to the user
mHandler = new Handler();
//mHandler.post(mRunnable);
// Run first add after 20 seconds
mHandler.postDelayed(mRunnable,20*1000);
}
protected void onStop() {
super.onStop();
// Stop showing Ad when activity is not visible anymore
mHandler.removeCallbacks(mRunnable);
}
private void loadInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}
private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
mNextLevelButton.setEnabled(true);
}
#Override
public void onAdFailedToLoad(int errorCode) {
mNextLevelButton.setEnabled(true);
}
#Override
public void onAdClosed() {
// Reload ad so it can be ready to be show to the user next time
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
});
return interstitialAd;
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and reload the ad.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
}
Make a new thread, then add a sleep time of 60000 milliseconds before showing your ad back. It will allow for async working.
Example, add this code in the onAdClosed() :
new Thread(new Runnable(){
public void run(){
Thread.sleep(60000);
// Show your ad here
}
}).start();

Add an interstitial admob ads without button pressed

I want to add an interstitial ads by admob without press any button but
it does not appear can you help I tried below codes:
...
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
mNewGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});
beginPlayingGame();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}
...
While loading an add you must add one device id that you are using for testing . while u r running this code see ur logcat to see the device id .. Copy and paste that device id into add test device and rerun u will start seeing add
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEViCE_ID").build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
beginPlayingGame();
}
the true answer here i found it
interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.interstitialAd.show();
}
}, 5000);
}} );

How to preload interstitial ads - android studio?

I created a small puzzle app, on completion of each level I wanted to display interstitial ads, but it is taking few seconds to load ads. So soon after the button is clicked it navigates to the next activity without showing ads. If I pause for few seconds and press the button it is showing ads. please help me out!
public class Completed extends AppCompatActivity {
InterstitialAd mInterstitialAd;
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to go to Home?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//if user pressed "yes", then he is allowed to exit from application
finish();
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//if user select "No", just cancel this dialog and continue with app
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
//AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completed);
requestNewInterstitial();
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxx");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
});
//requestNewInterstitial();
Typeface MyTypeFace=Typeface.createFromAsset(getAssets(),"comic.ttf");
Button btn=(Button)findViewById(R.id.btncomplete);
TextView txt=(TextView)findViewById(R.id.txtcomplete);
btn.setTypeface(MyTypeFace);
txt.setTypeface(MyTypeFace);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (MainActivity.setsound) {
MainActivity.mp.start();
}
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
finish();
}
});
}
}
on app start call init method:
public void init()
{
mInterstitialAd = new InterstitialAd(baseAppController);
mInterstitialAd.setAdUnitId(baseAppController.getString(R.string.interstitial_ad_unit_id));
mInterstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdClosed()
{
requestNewInterstitial();
}
});
requestNewInterstitial();
}
private void requestNewInterstitial()
{
mInterstitialAd.loadAd(getAdRequest());
}
public AdRequest getAdRequest()
{
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
return adRequest;
}
when you want to show ad just call below method:
public void showInterstitial()
{
if (mInterstitialAd.isLoaded())
{
mInterstitialAd.show();
}
}
use
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
}
public void onAdLoaded (){
//... your code is here
}
});

How to load Admob ads after certain time of app load?

Hi I am creating an Android app. I want to show Interstitial ads after some time of app load. Say 60 seconds.
The Code that I had developed shows add instantly as soon as the App is launced, I need that it should be shown after a delay of say 60 sec. But in the mean time the App should perform the operation and should not wait or sleep.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interAd = new InterstitialAd(this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayinterstitial();
}
});
}
public void displayinterstitial() {
if (interAd.isLoaded()) {
interAd.show();
}
}
Help me to resolve my issue.
Try this
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Your code to show add
}
}, 60000);
Your new code would be
#Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
interAd = new InterstitialAd(this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayinterstitial();
}
});
}
} , 60000);
}
Try this it could help you.
interAd = new InterstitialAd(this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
interAd.loadAd(adRequest);
}
}, 1000); // After one Sec

Categories

Resources