当我的服务运行TimerTask时,如何防止UI挂起?

时间:2012-01-07 09:19:45

标签: android service hang timertask

我有一个服务,其中包含一个Timer和TimerTask,用于在一段时间内从Webservice接收数据。当我的TimerTask运行时,UI会挂起,直到Webservice进程完成。如何将我的任务放在线程中以防止UI挂起?

我的代码:

Timer timerSyncFull = new Timer();

class taskSyncFull extends TimerTask {
        @Override
        public void run() {
            hSyncFull.sendEmptyMessage(0);
        }
    };



final Handler hSyncFull = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            procSyncFull();
            return false;
        }
    });

public void procSyncFull() {
        try {
            // My webservice process

        } catch (Exception e) {
        }
    }



@Override
    public void onStart(Intent intent, int startId) {

timerSyncFull = new Timer();

timerSyncFull.schedule(new taskSyncFull(), 5*60*1000,
                        5*60*1000);

}

4 个答案:

答案 0 :(得分:1)

使用AsyncTasks或将Handler附加到另一个Looper线程。

答案 1 :(得分:1)

我使用了以下代码并解决了我的问题:

class taskSendMapMovements extends TimerTask {
        @Override
        public void run() {
            hhSendMapMovements.sendEmptyMessage(0);
        }
    };



    // /////////////////////

    final Runnable rSendMapMovements = new Runnable()
    {
        public void run()
        {
            procSendMapMovements();
        }
    };

    final Handler hhSendMapMovements = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            performOnBackgroundThread(rSendMapMovements);

            return false;
        }
    });

    // /////////////////////

    public void procSendMapMovements() {
        try {    

        showToast("some text");
        //My Main Process    

        } catch (Exception e) {


        }
    }


@Override
    public void onStart(Intent intent, int startId) {
        try {



            timerSendMapMovements = new Timer();


            timerSendMapMovements
                        .schedule(new taskSendMapMovements(),
                                10*60*1000,
                                10*60*1000);

            //

        } catch (NumberFormatException e) {
            Toast.makeText(this, "error running service: " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this, "error running service: " + e.getMessage(),
                    Toast.LENGTH_SHORT).show();
        }
    }


final Handler hToast = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            Toast.makeText(SrvDataExchange.this,
                    msg.getData().getString("msg"),
                    Toast.LENGTH_LONG).show();
            return false;
        }
    });

private void showToast(String strMessage) {
    Message msg = new Message();
    Bundle b = new Bundle();
    b.putString("msg", strMessage);
    msg.setData(b);
    hToast.sendMessage(msg);
}

public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {

                }
            }
        };
        t.start();
        return t;
    }

答案 2 :(得分:0)

只需在thread或asyncTask中调用procSyncFull()方法。

final Handler hSyncFull = new Handler(new Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Thread thread=new Thread()
            {
                public void run(){
                    procSyncFull();

                }
            }
            return false;
        }
    });

private Handler webserviceCompletionHandler=new Handler()
{
    @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
};

答案 3 :(得分:0)

使用AsyncTaskdoInBackground()执行您的执行,并将其填入onPostExecute()

AsyncTask Example