I'm using Text to Speech on my app. After the google woman speak, a dailog box must be opened. The issue is: the dialog box open during the speech of the woman. So that's why I want to put a delay between the end of voice and the opening of the dailog box. I saw in several tutorials something about timesleep, handle, looper, thread and AsyncTask and I have a question: To my situation, what's the best way to aplicate? Thanks in advance.
In your case, I think AsyncTask will work best because of two reasons.
The women speaking part seems like it is a CPU-heavy process, thus it should be handled in a separate thread other than the UI Thread.
You can put your dialog on OnPostExecute() call and that call will trigger after the AsyncTask is done.
Hope it helps! Have fun.
Related
I'm trying to design an application that does two different tasks in parallel
each task start executing when the user presses a button in the activity "so I have three Activities two of them should do some tasks and the third one should collect the result".
When the user presses the button on the activity it will invoke a thread and then will Load the next Activity
In the third activity I have a button "I called it Send Button " that should stay inactive or disabled until all threads finish their work so at that moment it will be enabled and this activity contains plain text to show the result from these two threads and when the user presses this button it will send the information in the plain text to a web site.
My questions are : how can I run Threads in a different activity and send their result to another one? and how can I make that "Send Button" disabled until all threads finish their work.
I tried to make a while loop using a global variable in the onCreate method in the Third activity but it crashes the application and the activity didn't start up
You have two options to do a long running background task:
Using a thread which does your background work, with an singelton to access the results
Using a services which does the calculations
Depending on your actual use case the service is the first choice.
Use an Activity only when you need user interaction to your application. Else you can invoke both the threads using AsyncTask for your jobs(If you want the system to handle the worker threads for you. And unless you do not do any config changes on the fly -> performance hit).
Please keep in mind that, you should use AsyncTask only when the background jobs run for dew seconds. If you have a long running job, then use a service with a custom thread.
Hope this helped.
You can use AsyncTask. AsyncTask is one Class which is used to perform background processes without having threads. You can easily check the process is completed or not. Check this Tutorial. This can help you to understand how to run background process using an AsyncTask.
I have a jframe with "search button", when the user clicks it, a function will be called to search through several databases for the specified search criterion. What I want is to have a "wait/loading message" to appear while the function is searching, and disappears once the function is done executing. I searched for similar ideas and all of them are using thread programming. Can someone insight me if it is possible to use threads on my situation and if there are any other options?
thank you
You should perform the search in a background thread, and notify the UI using for example invokeLater.
You can find more informations and an example here http://www.javamex.com/tutorials/threads/invokelater.shtml
You could do the search process on the UI thread, but since it apearantly takes long to complete, the GUI would freeze. So for a fluid using expirience i would reccomend doing the search in another thread. Mulit-Thread programming is not as difficult as you might think.
See this tutorial
I have a GUI(JFrame), with two Buttons and 1 Panel to show the result. One Button is to start the algorithm, one for stopping it. By pressing start, a method is called and it starts running. The runtime of this method varies from couple of seconds to 2-3 minutes, depending on the input.
The problem I have hereby is, by pressing the start-button, the GUI gets completely locked. I cannot press any button till the algorithm terminates. It would be great to be able to stop the algorithm and to visualize parts of the solution after a certain amound of time.
I checked every single line of the Frame, there is nothing that disables it.
//If needed I can provide code, but its pretty long and just some hints and reasons for the problem would be great and I try to fix it by myself.
thanks in advance.
Don't put long-running tasks on the EDT, or the Event Dispatching Thread. Use threading or a SwingWorker instead. Hopefully that's enough google keywords to get you started. :)
It sounds like your algorithm is running in the same thread as the UI components. You probably want to read up on Concurrency and Concurrency in Swing to better understand how to create threads, monitor execution, integrating these concepts with a Swing-based user interface, and so forth. At a very high level, you are going to need to somehow spawn a new thread when your algorithm starts and observe it for intermediate state changes to update the UI. You only want user interface related code running in the event dispatch thread.
I have a sync service using AsyncTask. Due to its objective (sync), I prefer to block the user and show him a progressdialog. (And error if exists)
Another difficulty is that I have about 8 AsyncTask running simultaneously. So I can't do a simple call to the progress dialog page when I begin the work and a close when it's finished. It's more complex.
Can someone help me with that task ?
Regards
onPreExecute(), onProgressUpdate(Progress...) and onPostExecute(Result) in AsyncTask are invoked on the UI thread. You can use these to display a progress bar, update it as the syncing progresses and hiding it when the work is finished.
As to the 8 simultaneous async tasks, do you really need 8 concurrent tasks? Can't you run them sequentially on one background thread using a single AsyncTask?
In the first place the point of the Service is that you don't need/want to block user to do stuff because it happens in the background. To that aspect, a Service doesn't have a UI thread, so if you want a progress bar shown in your Activity you'll have to send an Intent back to your activity (using broadcast receivers), such that you can switch the progress bar on/off and do other magic.
I would not recommend blocking the user though, because the tasks you are doing might take a very long time, giving a nasty user experience. You might even want to reconsider using a Service at all (if the data you are fetching is only used locally; for example fetch the latest twitter messages or something) and just go with an ASyncTask in your Activity, unless the data your Service fetches is used in other parts of your app as well (widgets for example) and you want that data available even if the activity isn't running.
You can make use of progress dialog to show wait cursor kinda thing.
Also you can imitate the concept of CountDownLatch in your application to dismiss the cursor. Like you can have a static function in a class like updateTaskComplete and update a static counter. And once the counter is equal to number of async task then in the function updateTaskComplete cancel the progress cursor. I mean you have to do something on this line.
I have some questions about executing SQL queries (to an online database) from an Android app.
Since these are network operations and they are costly, should I be doing them in a separate thread? Also, what should I show the user (UI) while these queries are processing?
I suggest you to make a use of AsyncTask. In the doInBackground() method you'd be downloading/processing stuff. In the onPostExecute() you'll be displaying on the UI. This is in short. Research base on AsyncTask for further information.
The first answer is good with regards to AsyncTask. If they're REALLY long queries, I would put them in a service and communicate back to the activity with a broadcast, but if they're just "network long", then Async is good.
It seems like EVERYBODY wants to use a waiting dialog, but this kind of UI generally sucks. It blocks everything, so if you can't get a response or whatever, the user is stuck. I recently reviewed an app for somebody, and because our network was slow, the time spent waiting in an alert box was 47 seconds. Any idea how long that feels to a user?
I would disable a repost, put some kind of spinner up, but don't block the UI. If the user wants to do something else, let them. Also, when the AsyncTask comes back, the screen that it expects to manipulate may no longer exist. Wrap everything in case you get an exception.
I try to do all remote stuff in a service, even if it isn't totally necessary. Talk back and forth with broadcasts. That's just me, though ;)