To just do something every so often, you really shouldn't use a thread (or
UserTask), just Handler.sendDelayedMessage(), which is a lot lighter-weight.

On Wed, Mar 4, 2009 at 8:05 PM, Anh Khoa Nguyen Pham <[email protected]>wrote:

> Hi Clay,
>
> Thank for your help so much,
> Now, i can fix my problem.I insist to find way to fix my problem in my code
> because it will makes my skill better than using someone's code. But next
> time, i will use UserTask to do that.
>
> Best regard,
> NPAK
>
> On Thu, Mar 5, 2009 at 9:54 AM, Clay <[email protected]> wrote:
>
>>
>> you 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;
>>        mHandler.post(new Runnable() {
>>            public void run() {
>>                mProgress.setProgress(mProgressStatus);
>>            }
>>        });
>>        }
>>
>> 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.
>>
>> Respectfully,
>>
>> Clay
>>
>>
>>
>> On Mar 3, 11:55 pm, Anh Khoa Nguyen Pham <[email protected]> wrote:
>> > UserTask class is useful but in this case, if i want to fix my problem
>> in my
>> > code, can you help me to do that?
>> > I really appreciate your help !
>> > Thanks,
>> > NPAK
>> >
>> > On Wed, Mar 4, 2009 at 2:15 PM, Clay <[email protected]> wrote:
>> >
>> > > so 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:
>> >
>> > >http://code.google.com/p/apps-for-android/source/browse/trunk/Photost.
>> ..
>> >
>> > > 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();
>> > >                result.setActionStatus("FAILED");
>> >
>> > >                Action actionToRun = (Action)params[0];
>> >
>> > >                try {
>> > >                        result  =
>> > >                                RpcActionClient.executeAction(
>> > >                                        actionToRun,
>> >
>> > >  MomentaryNowApp.getString("app_momentary_now_server"),
>> >
>> > >  MomentaryNowApp.getInteger("app_momentary_now_server_port"),
>> >
>> > >  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;
>> > >        }
>> >
>> > >        @Override
>> > >        public void onPostExecute(ActionResult result) {
>> > >                Log.d(TAG, "result:"+result.getActionStatus());
>> > >                if(result.getActionStatus().equals("SUCCESS"))
>> > >                {
>> > >                        //get the progress bar to update
>> > >                        ProgressBar progressBar =
>> >
>> > >  (ProgressBar)findViewById(R.id.create_keyword_send_progress);
>> > >                        progressBar.setVisibility(View.GONE);
>> >
>> > >                        ImageView checked =
>> >
>> > >  (ImageView)findViewById(R.id.create_keyword_send_toggle);
>> > >                        checked.setVisibility(View.VISIBLE);
>> >
>> > >                }
>> > >        }
>> > >    }
>> >
>> > > 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.
>> >
>> > >http://developer.android.com/guide/samples/ApiDemos/src/com/example/a.
>> ..
>> >
>> > > 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.
>> >
>> > > Clay
>> >
>> > > On Mar 2, 11:32 pm, "[email protected]" <[email protected]> wrote:
>> > > > 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)
>> > > >     {
>> > > >         super(context,ats,ds);
>> > > >         init(context);
>> > > >     }
>> > > >     public My_View(Context context,AttributeSet ats)
>> > > >     {
>> > > >         super(context,ats);
>> > > >         init(context);
>> > > >     }
>> > > >     public My_View(Context context)
>> > > >     {
>> > > >         super(context);
>> > > >         init(context);
>> > > >     }
>> >
>> > > > public void change()
>> > > >     {
>> > > >         invalidate();
>> > > >     }
>> > > > private void init(Context context)
>> > > >     {
>> > > >         Resources res     = context.getResources();
>> > > >         mMoveObject        = res.getDrawable
>> > > > (R.drawable.lander_firing);
>> > > >         mBackground_img = BitmapFactory.decodeResource(res,
>> > > > R.drawable.my_pic);
>> > > >         mObjectw = mMoveObject.getIntrinsicWidth();
>> > > >         mObjecth = mMoveObject.getIntrinsicHeight();
>> > > >         Dx = Dy  = 0;
>> >
>> > > >         lasttime = System.currentTimeMillis() + 1000;
>> > > >         Thread mthread = new Thread(null,doBackground,"Background");
>> > > >         mthread.start();
>> > > >     }
>> > > >     private Runnable doBackground = new Runnable()
>> > > >     {
>> > > >         public void run()
>> > > >         {
>> > > >             long now = System.currentTimeMillis();
>> > > >             if(lasttime < now )
>> > > >             {
>> > > >                 Dx = Dx + 10;
>> > > >                 Dy = Dy + 10;
>> > > >                 lasttime = now + 1000;
>> > > >                 myHandler.post(change_view);
>> > > >              }
>> > > >         }
>> > > >     };
>> > > >     private Runnable change_view = new Runnable()
>> > > >     {
>> > > >         public void run()
>> > > >         {
>> > > >             change();
>> > > >         }
>> > > >     };
>> > > >     @Override
>> > > >     public void onDraw(Canvas canvas)
>> > > >     {
>> > > >         canvas.drawBitmap(mBackground_img,0 ,0 , null);
>> > > >         mMoveObject.setBounds(Dx, Dy, Dx+mObjectw, Dy+mObjecth);
>> > > >         mMoveObject.draw(canvas);
>> > > >     }}
>> >
>> > >
>> **********************************************************************************************************************
>> > > > Hope to see your reply soon,
>> > > > Thanks in advance,
>> > > > NPAK
>>
>>
>
> >
>


-- 
Dianne Hackborn
Android framework engineer
[email protected]

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to