When I run this code on a real device the ad-banner pops up endless times. How should i modifiy this code to run it just once?
This code is from a game. Every time the player gets hit by something he loses 1 shield.
private void showBanner() {
adView.setVisibility(View.VISIBLE);
adView.loadAd(new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}
if (player.getShieldStrength() == 0){
runOnUiThread(new Runnable() {
#Override
public void run() {
showBanner();
}
});
}
This my logcat:
Aborting last ad request since another ad request is already in progress.
The current request object will still be cached for future refreshes.
...
This runnable is triggered by the run method. showBanner is part of update-method
#Override
public void run() {
while (playing) {
update();
draw();
control(); }}
You are confused.
A banner ad is not like an interstitial. It does not pop-up.
When you request a banner ad you are requesting ads to fill that AdView until you tell it to stop or you hide it. It will show new ads on the refresh cycle you have configured in the Admob dashboard. It will not show a single ad.
If you really only want a banner ad to be shown until the next game, then call adView.setVisible(VIEW.INVISIBLE) or adView.setVisible(VIEW.GONE)
And please don't repost existing questions Ad pops up multiple times
Related
About a year ago i finished a super simple game in android studio and published it. Now i wanted to make a small update and show ads on more proper place. But ads won't work at all - code is the same as it was, but ads just won't load.
My account is not disabled since if I download my game from playstore ads still work.
I use different version of android studio, so the project upgraded itself, but I don't know if it changed graddle or manifest or something so this could happen.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxx/xxxxxxxx");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded () {
if(enableAd) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
}
});
Use onAdClosed() in your AdListener to reload your ad each time is has been displayed, and create your initial Ad as part of the mainActivity.create().
Use mInterstitialAd.show() anywhere in your app when you want to show the ad. After it is shown, it closes and will reload the next ad.
What this does is buffers your Ad. Whenever you show the interstitial ad, it shows the one that is already loaded, then reloads a new one after you've shown the ad. Makes for a quicker ad.show().
mInterstatialAd = new InterstitialAd(getBaseContext());
mInterstatialAd.setAdUnitId(getResources()
.getString(R.string.AdmobTestInterstitial));
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed () {
mInterstatialAd.loadAd(myAdRequest);
}
});
public void showMyInterstitialAd(){
if(mInterstitialAd!=null &&
mInstitialAd.isLoaded()) mInterstitialAd.show();
}
good luck.
This question is very similar to questions that have been asked in the past but please bear with me as it is still a unique question. Basically, I have a class that gets application permissions, and if the user does not have internet running, then when the auto login screen comes, it is stuck in loading. So what I want to do is show a dialog message, and the user will click ok to close the app. For dialogs, I need the context, and I must run on the main thread. I have posted an image of the code because I want to show you that runOnUIThread is red. Here is the error I get form Android Studio
Cannot resolve method runOnUIThread.
Here is what I had
Problem: For some reason, runOnUIThread is not usable. Does anyone have a counter proposal, or a reason as to why I am having this problem?
Here is the code:
public void alert() {
new Thread()
{
public void run()
{
application.runOnUiThread(new Runnable() // application is the context of my current activity.
{
public void run() //I display my alert Dialog here.
{
AlertDialog build= new AlertDialog.Builder(application.getApplicationContext())
.setTitle("Error")
.setMessage("Sorry there seems to be a problem with the service. Please check to make sure you have a stable internet connection. ")
.setPositiveButton("Ok, I understand.", (dialog, which) -> System.exit(0))
.show();
build.setCancelable(false);
Button positive= build.getButton(DialogInterface.BUTTON_POSITIVE);
positive.setTextColor(application.getResources().getColor(R.color.buttonPrimaryColor));
positive.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
}
});
}
}.start();
Here is how I have made it work with a Toast in the past.
public void shortToast(String msg) {
Observable.just(msg)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(message -> {
Toast.makeText(application, message, Toast.LENGTH_SHORT).show();
});
}
// In the main method
shortToast("Sorry an error occured");
For some reason, runOnUIThread is not usable
The method that you are trying to invoke is runOnUiThread(). That is a method on Activity. Whatever application is, it is not an Activity.
Does anyone have a counter proposal
Move this code into an Activity. Generally, pop-ups (dialogs, snackbars, etc.) should be displayed by an Activity. And only an Activity can show a Dialog, such as an AlertDialog.
Try to use an activity to run on UI, not application
I am writing n android app as part of my thesis and need to display/update/append data onto an android screen during a timer event - as follows:
(please excuse possible incorrect terminology and feel free to correct it)
I have a few classes in the app, however am trying to interact between "BlueActivity" and "BuzzActivity".
"BlueActivity" controls blue tooth. It turns on/off blue tooth,makes a connection and controls the socket.
"BuzzActivity" has a button "BUZZME": onClick, BuzzActivity passes (behind the sceens) into "BlueActivity" which sends a packet to the paired device (devDuino, who then radios out to a wearable device. the wearable device radios a reply of data readings back to devDuino). The paired device then replies this data back to "BlueActivity" which is read in through the socket listening on a timer. The data is in the format of a string and is called "retstr". The timer is started in "BlueActivity's" OnCreate() method.
I need to get "retstr" from "BlueActivity" into "BuzzActivity" and updating on the screen.
I have tried .setText and failed in the sense of App crashing. From research i hear about needing a handler to "dynamically" update the view?
Can some one please clarify this.
Code segments as follows:
BlueActivity:
.........//stuff here
//final String TAG = "On create";
new Timer().schedule(new TimerTask() {
#Override
public void run() {
//Log.i(TAG, "In timer running");
String retstr;
retstr = mConnectThread.listen(); //Method to get my data from bluetooth packet
//String text = String.valueOf(retstr); //failed attempts
//BuzzActivity.DivingData.setText(text);
Log.i(TAG, "retstr"); //Works like a charm to print out to log cat
}
}, 1000, 1000);
mConnectThread = new ConnectThread(mmDevice);
mConnectThread.start();
}
BuzzActivity
.......//blueConnecter is called from the .xmlfile controling the buzz button "on click method"
public void blueConnecter(View view){
DivingData = (TextView) findViewById(R.id.DivingDataTextField);
BlueActivity.visible(view);
}
Thanks for any help in advance.
Emma.
Is there a stack trace in the logcat output? You'll probably find that you are trying to update the UI thread from another thread. If that is the case, you need to update that view with runOnUiThread(). Here's an example:
runOnUiThread(new Runnable() {
public void run() {
DivingData.setText(newText);
}
});
I have a game with different screens. To show an ad on a specific screen I use the method below which use thread. Everything works fine but the ads keeps loading and new ads popup. I want that the ad loads only once.
How can I create a thread so the ad methods are called only once.
public void displayAd(){
runOnUiThread(new Runnable() {
public void run() {
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); // load the next ad
}
});
}
I have a long running method that is called during onCreate, this method populates textviews so interacts with the UI, and updates maybe 70 labels (about 3-20 seconds depending on device).
I want to display a progressdialog as this method executes.
Ideally I want to fire my method on the UI thread once the Activity has been displayed and the progress is displayed, this I cannot do, the Activity won't paint until the method has finished.
I hoped to find an event which was fired after the activity was displayed, and I found the one below, but it still leaves the screen black until the method has finished.
#Override
public void onPostCreate(Bundle savedInstanceState)
I am normally a WP7 developer, and in .NET you add an event handler for onLoadComplete which is fired after the ui is displayed, but before the user has a chance to interact withthe UI, how do I do this in Android JAVA?
Thanks
Put a ProgressBar in the View.
Then in the onCreate() or onResume() method do this:
new Thread() {
public void run() {
yourLargeMethod();
}
}.start();
Now you can do this inside your method to update the progressBar
public void yourLargeMethod() {
// doSomething
...
runOnUiThread(new Runnable() {
public void run() {
// update the progress from the thread
progressBar.setProgress(x); // x is your progress, 0 <= x <= progressBar.getMax()
}
});
}