How to update View object on main thread by background thread

by » Wed, 04 Mar 2009 02:01:18 GMT

Sponsored Links
 Hi all,
My purpose is very simple : each 1second, I want to redraw an object
on different place on background.
I do not know where my error on this code below. Hope that you can
show me or give me an advice to fix this problem

Here is my code
public class My_View extends View{

    private Bitmap mBackground_img;
    private Drawable mMoveObject;
    private int mObjectw,mObjecth;
    private int Dx,Dy;

    private Handler myHandler = new Handler();
    private long lasttime;
    public My_View(Context context,AttributeSet ats,int ds)
    public My_View(Context context,AttributeSet ats)
    public My_View(Context context)

public void change()
private void init(Context context)
        Resources res     = context.getResources();
        mMoveObject        = res.getDrawable
        mBackground_img = BitmapFactory.decodeResource(res,
        mObjectw = mMoveObject.getIntrinsicWidth();
        mObjecth = mMoveObject.getIntrinsicHeight();
        Dx = Dy  = 0;

        lasttime = System.currentTimeMillis() + 1000;
        Thread mthread = new Thread(null,doBackground,"Background");
    private Runnable doBackground = new Runnable()
        public void run()
            long now = System.currentTimeMillis();
            if(lasttime < now )
                Dx = Dx + 10;
                Dy = Dy + 10;
                lasttime = now + 1000;
    private Runnable change_view = new Runnable()
        public void run()
    public void onDraw(Canvas canvas)
        canvas.drawBitmap(mBackground_img,0 ,0 , null);
        mMoveObject.setBounds(Dx, Dy, Dx+mObjectw, Dy+mObjecth);
Hope to see your reply soon,
Thanks in advance,


How to update View object on main thread by background thread

by Clay » Wed, 04 Mar 2009 15:15:40 GMT

 o just from the basic use case I dont know that I would use a
handler, basically I really like Romain Guy's implementation for
background user tasks that do things and then update the screen, its
based on his userTask implementation:

so to extend this, it uses generics *very wisely* so you can make any
model you want for both the doInBackground params which returns the
third arg, and becomes the param for onPostExecute.

* Background task to verify the create user action. When the
action comes back
* it could inform the client to sync all data if the username and
password match
* or that there was a validation error like the user already
exists, or that
* this is a new user and that it successfully created it.
private class PostActionTask extends UserTask<Object, Void,
ActionResult> {
public ActionResult doInBackground(Object... params) {
ActionResult result =
new ActionResult();

Action actionToRun = (Action)params[0];

try {
result =



MomentaryNowApp.getString("app_momentary_action_endpoint")) ;

} catch (IOException e) {
Log.e(TAG, "IOException:"+e.getMessage());
} catch (HttpException e) {
Log.e(TAG, "HttpException:"+e.getMessage());
} catch (URISyntaxException e) {
Log.e(TAG, "URISyntaxException:"+e.getMessage());
return result;

public void onPostExecute(ActionResult result) {
Log.d(TAG, "result:"+result.getActionStatus());
//get the progress bar to update
ProgressBar progressBar =


ImageView checked =



secondly it is my understanding that AlarmManager is the timer
infrastructure you would most likely use
and that it would then call your background task.

I am sure there are other ways to do this but this would be the way I
would try if I was doing what you told me.


On Mar 2, 11:32pm, "" <> wrote:

Sponsored Links

How to update View object on main thread by background thread

by Clay » Thu, 05 Mar 2009 10:54:30 GMT

 ou have two threads one you post, the other you start...

again I wouldnt endorse the way your trying to manage your threading
in this case, and Romain (on the developer team) seems to recommended
doing background updates to activity views using update task so I
think you might benefit by rethinking your approach.

generally I would post runnable to handlers like this *if you must*

private void updateProgress(int amount){
// Update the progress bar
mProgressStatus = amount; Runnable() {
public void run() {

So basically it is possible to hammer that square peg into your round
hole if you wish, I just wont be the one to do it.



On Mar 3, 11:55pm, Anh Khoa Nguyen Pham <> wrote:

Other Threads

1. Cant locate some apps

I have been on the android market for days now and can't find some
apps. Locale is one of them. I looked through the "tools" area until
it would scroll no more. I also did a search and only came up with
"Locale SMS", "Loot", and "BuzzOff". Locale is supposed to be in the
market but where is it? Am I missing something here? There are a few
others that should be there but they're not.


2. Grayscaling


I am trying to grayscale bitmap as follows, But it is crashing as soon
as I run this method.
The problem is in last line. How can I assign "int" to "Color"? Or are
there any better ways? Thx

for(int y = 0; y < imgH; y++)
for(int x = 0; x < imgW; x++)
                int color = bitmap.getPixel(x, y);

                int r = (color >> 16) & 0xff;
                int g = (color >> 8) & 0xff;
                int b = color & 0xff;

                color = Color.rgb(r/3, g/3, b/3);

                bm.setPixel(x, y, color);


3. Canvas.drawBitmap() src/dst Rect precision

4. Control which items to redraw? (notifyDataSetChanged with a range)

5. Android default color madness, primary_text_light is color black?

6. breakpoints and widgets

7. Accessing java SIM card information from android