3

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

4 回答 4

1

使用 AsyncTasks 或将您的处理程序附加到另一个 Looper 线程。

于 2012-01-07T09:38:27.177 回答
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;
    }
于 2012-01-08T05:55:13.307 回答
0

只需在线程或 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;
        }
};
于 2012-01-07T09:39:21.120 回答
0

使用AsyncTask执行您的执行doInBackground()并将其填充到onPostExecute()

异步任务示例

于 2012-01-07T16:46:22.620 回答