Android :: Handler Vs AsyncTask
Mar 26, 2010I'm confused as to when one would choose AsyncTask over a Handler. Say I have some code I want to run every n seconds which will update the UI. Why would I choose one over the other?
View 3 RepliesI'm confused as to when one would choose AsyncTask over a Handler. Say I have some code I want to run every n seconds which will update the UI. Why would I choose one over the other?
View 3 RepliesI'm trying to figure out the correct way to create an AsyncTask to retrieve some data from the internet and then to take that data and bundle it up in an Intent and pass it to a new activity(A list display). So in the first activity I just have an EditText and Button. In the event of an OnClick the task should be called and when it is finished the data should be bundled inside an Intent and passed to the next Activity. The problem is when I take the results from onPostExecute and set them to an instance variable of the main activity, that instance variable is still null when the task is complete. Here is the barebones version of the code:
CODE:...........
When I debug the application I see onPostExecute does contain a valid PlacesList full of results, so why is the instance variable places set to null after the task is executed? I am going about "returning data" from an AsyncTask incorrectly?
I am working on an application wich has its own URI prefix. (dchub:// in this case) is there a way when someone open his browser, clicks on a link starting with dchub:// my app starts using this address? so far found a lot of examples the otherway around opening the browser from your app but thats not what i'm looking for.
View 1 Replies View RelatedI've got 2 classes GLLayer and GLCamTest. I'm attempting to run a method located in GLCamTest...
CODE:..........
I'm looking to run in on a thread from GLLayer but from what I understand I need a Handler..
CODE:.........
I'm starting the Thread from within public void onDrawFrame(GL10 gl) { my question is how would I implement said handler? I've read http://developer.android.com/reference/android/os/Handler.html but I still don't really understand how I'd implement it.
I am very confused about handlers and looper can anyone plz explain it in simple terms to me any good links would be great.
View 3 Replies View RelatedI'd like to know if Looper can enable handler to handle one message at a time, instead of handling all the messages in the queue together in one blocking call when Looper.loop() is called.
Because the looper sends all the messages in the queue to the handler one after the other in one blocking call, my application displays the ANR message. Instead, I want to be able to handle one single message, one blocking call at a time - the instant it falls into the queue.
The following code illustrates my problem: (pls note the lines referred: 1,2,3,4&5) Question 1: After calling looper.loop in line3, line4 and 5 don't run. Why is this? I tried quit(), but it doesn't help. Question 2: Instead of displaying 0,1,2,3,4 in one blocking call (at line3), I'd like to have 5 different blocking calls for each. Essentially (sorry, if i sound repetitive), I want to be able to use looper in such a way that each time, it ensures only one message gets handled.
Is there a way to do this?
CODE:..................
I've read the android AIDL documentation and have a general idea of how RPC works between an Activity and a Service. However, for my application it seems overboard to implement such features: basically, I want to pass a Service a nice handler so its thread can pass data to my Activity. Currently I'm getting around this by using a static public member (a hack) but I would prefer just passing a Handler object in the Service's starting Intent. However since a Handler isn't serialize-able , I haven't found a way to pass it to the service without a simple static member hack. Any insight? Or, am I just going to have to suck it up and do a formal RPC to the service?
View 1 Replies View RelatedI am using a handler inside my service to display notifications at a certain time using the Handler.postDelayed method. My application runs fine on the emulator just as expected satisfying all cases. But when I installed the same on my HTC Wildfire, it simply doesn't happen as anticipated. Notifications are displayed at a random manner after the scheduled time and some notifications even fail. I'm monitoring my service from the Applications > Running Services and still my service is active.
View 1 Replies View Relatedi want to do the following thing - when a user navigates to a web page, I want to have a link in there that looks like this: <p><a href="myprotocol://blah=42">Click here for fun.</a></p> Then when the user clicks on that from the phone browser, I want it to launch my intent with that full url. In my AndroidManifest.xml file, I have the following info: <activity android:name="com.test.MyActivity" style="@style/ MyStyle"> <!-- custom protocol association --><intent-filter><action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category><category android:name="android.intent. category. BROWSABLE"> </category><data android:scheme="myprotocol"/></intent-filter></activity>When I try this, when I click on the link on the web page from the phone browser, it says -- Web page not available The Web page at myprotocol://blah=42 might be temporarily down or it may have moved permanently to a new web address.
View 20 Replies View RelatedI'm trying to call a web server that is using relative URL redirects for some of the calls. This of course isn't working with DefaultHttpClient as it isn't treating it as a relative URL. I've gotten as far as implementing a RedirectHandler in an attempt to catch the redirect and add in the base call but I can't work out how to get the location of the redirect.
With the following method how do I go about finding out where I am being redirected to? I can't find any fields on either response or context that have what I need and I don't know where else to look.
public URI getLocationURI(HttpResponse response, HttpContext context)
Assume that there activity A and B, A has a handler, now B is activate by A, how can i pass the handler to B? Currently i have a thread receive messages from socket, and dispatch messages to A and B, I think when B is activated, pass handler to B, then the handler can receive message from the thread. But I don't know how to do.
View 3 Replies View RelatedI need to update my ui for an android app and I'm trying to use the Handler class to do it, using http://developer.android.com/resources/articles/timed-ui-updates.html and the android developer resources "Common Task" for using Handlers as guides.
Basically, I need something between the two - a timed update of the user interface, but without a button. So here is the relevent code that I am working on.
CODE:...............
I will try to keep it simple:
In my main activity I make a handler:
CODE:.......
CODE:......
But if I call the handleMessage method from a callback function in a other Class, definitely from a other thread, I still get the exception message: CalledFromWrongThreadException (Only the original thread that created a view hierarchy can touch its views) :
CODE:...................
I created a full-screen application and set the default orientation to landscape. When I open the keyboard the application crashes therefore I would like to override the method which executes on keyboard slide. Does anyone know which method is that?
View 1 Replies View RelatedI need to download an image from the internet,in a different thread,and then send that image object in the handler message, to the UI thread.And by the way, is this the most efficient way to pass an object to the UI Thread?
View 2 Replies View RelatedIn Handler, we can pass some data from a background thread to the UI thread like this:
private void someBackgroundThreadOperation() {
final String data = "hello";
handler.post(new Runnable() {
public void run() {
Log.d(TAG, "Message from bg thread: " + data);
}
}
}
If we use the above, we cannot then use Handler.removeCallbacks(Runnable r), because we won't have references to any of the anonymous runnables we created above. We could create a single Runnable instance, and post that to the handler, but it won't allow us to pass any data through:.............
I made a simple countdown timer and it works as expected when plugged in via usb for debugging but when I take it off debugging and the screen goes off either time out or power button the handler fails to fire at the end time. I have created a custom timer class that gets created from the main activity when needed, because its able to have multiple countdowns running at the same time. Am I right in thinking that the handler just gets paused when the screen goes off and if so what are my alternatives. The activity is still in the foreground and I assumed the handler would still fire if the screen goes off.
View 13 Replies View RelatedMy program threw a NullPointerException the other day when it tried to use a Handler created on another thread to send that thread a message. The Handler created by the other thread was not yet created, or not yet visible to the calling thread, despite the calling thread having already called start on the other thread. This only happens very rarely. Almost every test run does not get the exception. I was wondering what the best way is to avoid this problem for sure with minimal complication and performance penalty. The program is a game and very performance sensitive, especially once it is running. Therefore I try to avoid using synchronization after setup, for example, and would prefer to avoid spinning on a variable at any time.
View 2 Replies View RelatedThe first time issue it i give null as parameter to the function to the selection parameter is empty, meaning i don't filter any rows. The problem is i get only contacts i created myself using no syncAdapter. I used facebook app to synch my facebook contacts, but this query doesn't return them. I extracted the contacts.db from the emulator and the view_contacts view shows me all the contacts, so the DB is updated. What should i do to get all the contacts regardless of how they were created (with which synch adapter)
View 1 Replies View RelatedI wish to analyze the various data passing through the browser. I would need a handler of the browser to query the contents. Is there any way that my application can get this handler of the browser and query for these data.
View 5 Replies View RelatedI have to post messages from one process to another process by using handler .
I am using Handler.post/Handler.sendMessage but i am not getting those messages in another process. I am not getting any exceptions also.
Is it possible to sent messages across processes using Handler?
If it is the case how can i do it?
I have implement the following code in order to test playing a video from a remote web server through itīs URL.
CODE:........
The code is working just fine, even in the Android emulator. I just would like to know if there's any listener (or handler) to detect the finish of the video that is being reproduced?
I would like to show a number of alertdialogs so the user has to handle off some questions (a little like a wizzard)
Is it possible to make the alertDialog wait until the user chooses something and then returning the choisen value?
CODE:.........
The execution of the program should wait until the user chooses 1 of the options in the alertDialog is this possible?
Like this:
CODE:..............
I have a class that uses a Handler for a timed, asynchronous activity.
Something like this:
CODE:.................
I seem to be having trouble with updating a TextView from a thread. I have a GameConnection class (which manages a socket connection) which I want to use across activities. It calls a local "onMessage", which then uses the target handler to call dispatch Message. The "Handler" in this case, is in my GameBrowser activity.
Here's code from the GameConnection class.
CODE:......
As said above, a local method "onMessage" method handles dispatching of the message.
CODE:.......
However, when I get the response in the GameBrowser class, I get a CalledFromWrongThreadException . Initially, I was using a callback method, which of course wasn't working. So, after some research, I've found that I have to use a Handler, but I can't seem to get it right.
CODE:........................
I am working on a simple application (studying purposes) which list all the files from a selected folder. On top of that I would like to have a search feature where the user can search for files (the code for that is already in place). Now, I was thinking about having the search running in the background somehow, whilst the user can still navigate, create folders, copy, sort and do other stuffs normally. When the search finishes the user would get a notification and then could click on it and go to that activity (It ideally should be the same ListView I already use for browsing the files, I would just need to update the Adapter there with the latest processed data after clicking in the notification). What's the best answer for that? Threading or AsyncTask?
View 4 Replies View RelatedCan anyone point me to a good example where a AsyncTask queries a local SQLite database and then updates the UI successfully. I have a database which I query using doInBackground... create a new custom SimpleCursorAdapter (overriding onViewBind), return the adapter (SimpleCursorAdapter), then in postExecute() create ListView adapter which is set to the the SimpleCursorAdapter, that was returned, then I set the listview adapter using SetAdapter(adapter);...
For some reason I throw an exception on fillWindow.java:200....
My application parse an xml file dans display data in a list view. On start of application i load data using an AsyncTask. A Progress Dialog is display during the load. This part works fine.A button in the application make it possible to reload the data. I would like to run the AsyncTask but the sytem say i can't alter view in other thread. I have also read an AsyncTask can't be run another time So i would like to know what is the best way to do this and not to have the "application not responding" message.
View 5 Replies View RelatedI know this is not how an async task should behave but my question is how to "block" the user while executing it.My need is the following: I have my own backup/restore process and I have an async task to run these two actions. The backup is fine, I can warn the user when the backup is done and that's just fine But my problem is about the restore process. When the user click on restore he shouldn't be able to make any change in the application (and anyway don't want to because he would lose all his changes).I understand that having the user blocked while restoring (or maybe with a progress bar) is not a best pratice but I do not see any other possibility in this context.
View 7 Replies View RelatedI know how to use AsyncTask in a standard manner to manage operations that are in the background in relation to a UI thread.However,I want to run a task in the background which might run for a very long time under certain circumstances. In these cases, I would like to force the background task to fail if it runs for an excessive amount of time.I know that I can invoke the "get(long timeout, TimeUnit unit)" method of AsyncTask in my UI thread in order to terminate my background task if it runs too long. However, in that case, my UI will block while this "get()" command is waiting.There are probably other drawbacks to directly calling "get()" in this manner, not the least of which being an evil interaction with the "done()" method of AsyncTask's contained FutureTask object, which itself is calling "get()" at least this is what I see when I look at the source code for AsyncTask.
View 10 Replies View Related