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
}
});
}
Related
I have two activities in my Android app.
1) A list which shows items
If the user clicks on one item it opens activity 2
2) Shows a big image of the item
Now, when I click on an item and thus start the second activity there are a few seconds which the app needs to start the activity. (I suspect because of the image it needs to load). This completely destroys the flow of the use case.
What is the best practice to deal with this?
Loading Bitmaps on Main UiThread takes time so you have to load your bitmaps off the Ui Thread. Use the Universal Image Loader for loading/caching the bitmaps. See the link https://github.com/nostra13/Android-Universal-Image-Loader
ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view
// which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);
Hope this helps.
In Second Activity use new Thread.. like below
new Thread(new Runnable() {
#Override
public void run() {
//here put your code
runOnUiThread(new Runnable() {
#Override
public void run() {
//here set your image.
}
});
}
}).start();
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
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 am writing here to ask you a litlle question about the progress dialog.
In fact, i call web service in my application, the progress dialog works very well.
But when i change the date of my object to do the call of my web service,
the progress dialog appear, the results are receiveid. AND automatically, there is a new call of the web service and the progress dialog didn't dissmiss :-/
On my server JBoss, i can see the calls from my application.
At the first create of my activity i have one call.
But at the second call, when i changed the date, there are two call. i don't know why because when i change one of my spinner its works fine, the web service is call and returns the results.
I call the web service in a asynctask. I do nothing with the progress dialog in the asynctask.
Here is the method where i call the progress dialog
public void callWebService()
{
myProgressDialog = ProgressDialog.show(OverviewMoney.this,"", "Récupération liste des positions cash",true);
final Runnable runInUIThread = new Runnable() {
public void run() {
setListView();
}
};
new Thread() {
#Override public void run()
{
loadListMoney();
myProgressDialog.dismiss();
uiThreadCallback.post(runInUIThread);
myProgressDialog= null;
}
}.start();
}
The method loadListMoney call my web service in a asynctask and the method setListView put my resultList in an adapter to print the list on a list view.
Thanks in advance :-) (And sorry for my english)
Try this
public void callWebService()
{
if (myProgressDialog != null) myProgressDialog.dismiss();
myProgressDialog = ProgressDialog.show(OverviewMoney.this,"", "Récupération liste des positions cash",true);
final Runnable runInUIThread = new Runnable() {
public void run() {
setListView();
}
};
new Thread() {
#Override public void run()
{
loadListMoney();
myProgressDialog.dismiss();
uiThreadCallback.post(runInUIThread);
myProgressDialog= null;
}
}.start();
}
According to my understanding, when this function is called twice 1st time it will start progress dialog and quickly another function call is placed so without dismissing previous progress dialog you were creating one more progress dialog. So that was the main cause of it.
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()
}
});
}