Android :: Handler Class And The Timing Of When Its Message Queue Is Emptied
Sep 6, 2010
I was curious about the nature of the handleMessage() and sendMessage() behavior of the Handler class. I want to be able to send message to another thread in such a way that the destination thread can process the message queue when it wants to. It seems, however, that the message is processed by handleMessage() practically as soon as it's sent.
I'm trying to design a game loop thread that does something like this:
CODE:.....
However, I as soon as sendMessage() is called (from the parent/calling thread), the Handler.handleMessage() is processed (in the child/receiving thread), even if the child/receiving thread is blocking in a while loop.
I've seen this problem solved in other games by using a thread-safe list (ConcurrentLinkedQueue). The UI thread just posts events to this queue, and the game loop can remove the events as it seems fit. I just assumed the Handler class was designed for this purpose. It seems it's more intended for asynchronous callbacks to the parent thread.
View 1 Replies
Oct 7, 2010
I am queuing up a bunch of runnables into a thread via a Handler.post(). I would like the ability to send a note to that thread that it should pause. By pause I mean, finish the runnable or message you are currently working on, but don't go to the next message or runnable in your message queue until I tell you to continue.
View 2 Replies
View Related
Mar 30, 2010
I'm writing unit tests for a ListActivity in Android that uses a handler to update a ListAdapter. While my activity works in the Android emulator, running the same code in a unit test doesn't update my adapter: calls to sendEmptyMessage do not call handleMessage in my activity's Handler. How do I get my ActivityUnitTestCase to sync with the MessageQueue thread and call my Handler?
View 2 Replies
View Related
Oct 30, 2010
I 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:...............
View 3 Replies
View Related
Oct 5, 2010
I have a class that uses a Handler for a timed, asynchronous activity.
Something like this:
CODE:.................
View 11 Replies
View Related
May 2, 2009
Instead of finding the button (as per the id specified in xml) i.e
CODE:.....
Is it possible to set the Listener class name directly in the xml definition. Most of the time dealing with id's for attaching listeners is affecting our productivity.
View 5 Replies
View Related
Mar 18, 2010
What is the difference when a class extend from Handler and Thread?
As described in developer.android.com
...
Each Handler instance is associated with a single thread and that thread's message queue.
...
Does the thread has no message queue ?
Any benefit for a class extend from Handler?
View 2 Replies
View Related
Jun 9, 2010
I 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 Related
Feb 18, 2010
I have an app with a two threads - main and data loader. When data loader finishes it posts a Runnable object to the main thread (as described in the DevGuide), but it never gets delivered and run. Here's the basic code:
class MyApp extends Application{
public void onCreate()
{LoaderThread t = new LoaderThread();
t.start(); }
private class LoaderThread extends Thread {
public void run()
{ SystemClock.sleep(2000);
boolean res = m_handler.post(m_runnable);
if(res)
Log.d(TAG, "Posted Runnable"); } ............
View 2 Replies
View Related
Oct 7, 2010
connect / login are executed in the same thread of the caller, then are blocking - The packetlistener callbacks are called from smack receiver thread - The sendpacket enqueues the message to a queue. when I receive a message, I need to propagate it. I can use an handler, but it only gets the runnable object. How can I pass the data to the runnable called by the handler? I can't use a local variabile, because if I receive a lot of messages it will be overridden and I will loose some of them. Is it ok to use a queue to feed the runnable? Are there other techniques?
View 3 Replies
View Related
Sep 4, 2010
I am using an Executor [fixed thread pool] with my own ThreadFactory that adds a Looper:
CODE:...............
I am running a thread that makes network requests but if the network fails I would like a dialog message to be displayed to the user. This process is rather involving since it requires making AND displaying the request in the UI thread. I can wait for the user's response to the dialog by simply adding a Loop to the network thread and wait for a message to be send from the UI thread. This allows me to encapsulate the network requests in a while(tryAgain) thread. All works well except when the Looper.loop() method is called the second time (after a second network error dialog is displayed) and a message is sent by the dialog (in the UI thread) to the network thread's handler:
CODE:.............
In the AlertDialog instance is an OnClickListener:
CODE:...............
I've checked that the thread is still active with handler.getLooper().getThread().isAlive() which always returns true but it still gives me "sending message to a Handler on a dead thread". How is it that the Message/Handler has decided that the thread is dead? Shouldn't it rely on the .isAlive() method? In the end I am trying to avoid replicating the thread management build into the Android OS .
View 1 Replies
View Related
Oct 30, 2010
I 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:...................
View 1 Replies
View Related
Jun 11, 2013
Weirdest thing - I delete all the data from S Planner, and it's persistently coming back
If I go to Settings>App Manager>S Planner, stop the app and clear the data (and cookies), it's still in the app. Even if I restart the device (heck, I even took the battery out overnight) - I can still see all my appointments
Unfortunately I have about 5000 entries in my calendar so manual deletion is not an option
View 1 Replies
View Related
Aug 31, 2010
For some reason, my inbox for my IMAP connection to the office email decided to empty itself on my Desire (2.1).An email arrived and I opened the inbox (not the email) and my inbox was empty.I have just popped in to the office (day off - typical!) and all my email is still there so it's not a total loss, just a little confusing since everything was working sweetly.The email server is based on an OS X 10.6.4 server in the office (not that this should have anything to do with it really).I've been through checking the settings and they're all OK. Only thing I haven't tried is removing the account and putting it back on. Might give that a try later on.
View 1 Replies
View Related
Sep 27, 2010
When using torrent-fu, does the computer (doing the downloading) have to be on? Or can I just use torrent-fu to find torrents when I'm at work, queue them up, and when I get home turn the computer on and it'll start the downloads?
View 1 Replies
View Related
May 5, 2010
I need to store a queue into a DB. I need to be able to insert at the head, and append at the tail. I also need to remove items from the head.
Each item is just a string. I need to maintain a todo list -- high priority items are added to the beginning of the queue. and low priority items are append to the end.
View 2 Replies
View Related
Nov 27, 2009
How can I keep Toast from building up a queue of messages?
I'm working on a board game and I'm sending status messages back to the user with this wrapper function I created for Toast:
CODE:...........
The problem is I've made the flow of game play so simple I can now play it so fast Toast messages queue up, even to the point where they continue to show on the desktop after I quit out of the game. t.cancel() doesn't seem to cancel out the previous message, anyone know what I might be doing wrong?
View 13 Replies
View Related
Sep 29, 2010
Is it possible, with an IntentService, to send another intent to the IntentService from within the IntentService? For example, say my IntentService is processing an Intent which has it write a bunch of data to the database. During this time, several other Intents to write other data may [or may not] have been queued up in the IntentService. Suppose, after processing that Intent by writing the data to the application's database, I want to queue up another Intent to send that data via web service to "the cloud." Perhaps I want to queue this processing in another Intent because sending to the cloud is secondary. Is it possible? Would it cause any problems if the Intent being processed is the only Intent in the queue at the time the IntentService is trying to queue another Intent?
View 2 Replies
View Related
Nov 14, 2009
How can I tell what has and has not bee downloaded? I see options to add to my download queue subscribe...but no option to see what's in the queue or manage what's been downloaded.
View 1 Replies
View Related
Jun 12, 2010
I read somewhere (and have observed) that starting threads is slow. I always assumed that AsyncTask created and reused a single thread because it required being started inside the UI thread.The following (anonymized) code is called from a ListAdapter's getView method to load images asynchronously. It works well until the user moves the list quickly, and then it becomes "janky".final File imageFile = new File(getCacheDir().getPath() + "/img/" + p.image);image.setVisibility(View.GONE);view.findViewById(R.id.imageLoading).setVisibility(View.VISIBLE);
(new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
try {Bitmap image;
if (!imageFile.exists() || imageFile.length() == 0) {
image = BitmapFactory.decodeStream(new URL(
"http://example.com/images/"
+ p.image).openStream());
image.compress(Bitmap.CompressFormat.JPEG, 85,
new FileOutputStream(imageFile));
image.recycle();
}image = BitmapFactory.decodeFile(imageFile.getPath(),
bitmapOptions);
return image;
} catch (MalformedURLException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
} catch (IOException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
return null;
@Override
protected void onPostExecute(Bitmap image) {
if (view.getTag() != p) // The view was recycled.
return;
view.findViewById(R.id.imageLoading).setVisibility(
View.GONE);
view.findViewById(R.id.image)
.setVisibility(View.VISIBLE);
((ImageView) view.findViewById(R.id.image))
.setImageBitmap(image);
}}).execute();I'm thinking that a queue-based method would work better, but I'm wondering if there is one or if I should attempt to create my own implementation.
View 2 Replies
View Related
Sep 11, 2009
I decided to start pipelining input to increase responsiveness in my games. Basically what I do is when the activity receives a Key or Motion event, it sends it to a feed method in the game, which encapsulates it with a time and whatever else I need and puts it in a queue then immediately returns so the UI loop is never held up waiting for long. The main loop later processes the queue, applying the events in it at the appropriate times.
This works great and makes the games snappy, although now I'm starting to miss touch events intermittently. I realized that the events could be recycled objects and that could make them not safe to queue. Is this the case or is it safe to queue them for deferred processing like I'm doing?
View 5 Replies
View Related
Apr 9, 2010
I 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
Sep 20, 2010
I'm running a network service within an ASyncTask. I want to be able to time the task, and after a certain period of time interrupt it.Is there a simple way to do this? Basically, when the doInBackground() methods starts, I want to say "If it hasn't completed in 30 seconds, do something else".
View 2 Replies
View Related
Jan 19, 2010
I wanted to ask a question regarding the Alarm Manager. I have a requirements of setting long running timers in my application. The minimum duration for my timer is 1 hour...and the maximum may be for 4-5 days. This timer is supposed to run in the background, and when it fires/expires, i have to show something on the GUI. The timer can be re-scheduled once it fires based on user action.
What is the best approach for scheduling such long running timers ?
Should i make use of the Alarm Manager ? Or will it be an overkill ? As far as i know, alarms are retained even if the device goes to sleep and they wake up the device when the alarm goes off.
I am thinking of using a simple Java TimerTask as an alternative to setting an alarm: http://java.sun.com/j2se/1.5.0/docs/api/java/util/TimerTask.html.
But i am not sure what will happen to the timer task if the device went to sleep while the task was running.
View 5 Replies
View Related
Oct 5, 2010
I'm doing my thesis work on resource management on android and I want to be able to measure the "responsiveness" of an application to figure out if the application get enough resources and is scheduled often enough.
So what I want is a way to insert timestamps on events, e.g. button clicks, when they are created and when they are consumed. This way I can get the "responsiveness" of the application. But I have a hard time of finding exactly where the events are created and also consumed.
In Handler.java there is the handleCallback(Message message) method which I can insert the end time stamp. I tried to add a long variable in the Message class and that worked well (had to rebuild the api though). So my problem is to find the place where the button click is generated and where the message that corresponds to that event is being created so I can add a time stamp.
I also welcome other suggestions on how to measure the "responsiveness" of an application or other way to measure how well an application performs.
View 2 Replies
View Related
Feb 29, 2012
Why the time above the adroid bat graph and the one below are different. At this moment is says on my device 1h 28 minutes on battery. Below the graph 50m 22s is shown. What is the difference between those timers ?
View 2 Replies
View Related
Dec 4, 2009
Are there any Android APIs that allow the application to retrieve the GSM Timing Advance?
View 2 Replies
View Related
Jul 7, 2010
I'm creating a new class, using eclipse "New Java Class" dialog box. I can write the superclass I want (I can't find using "browse" button), but I can't write or select an interface to implement. I click "add" but ther is nothing to select. What I'm doing wrong?
View 4 Replies
View Related
Jul 14, 2010
I'm just getting into Android development, and I have a question about communicating between a receiver class and an activity class. I'm very new to JAVA and Android so I hope I don't sound too stupid. I'm developing an application where I intercept an SMS message and then based on various elements of that SMS I might delete it once it's been saved to the inbox. I have a receiver class that intercepts the txt message, and I am also able to delete messages from my inbox with code in the activity class using a button at the moment. The problem I have is communicating between the receiver class and the activity class where the code to delete a message resides. I tried putting that code directly into the receiver class but as I'm sure most of you already know the BroadcastReceiver class doesn't seem to support what I need to delete messages. I've been searching for an answer to this for a while, but haven't been able to find anything. Honestly I'm not sure I know enough about JAVA and Android to even recognize a solution if I saw it.
View 2 Replies
View Related
Jun 27, 2010
What I want to do, is be able to access the object neoApi inside the Neoseeker class, from its inner class RunningTimer. Now, in my code, you can see what I would think to work, but when I run my application, nothing pops up. Nothing inside my TextView, no Toast, nothing at all. How can I remedy this?
package com.neoseeker.android.app;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;.......................
View 1 Replies
View Related