开发者

Thread in android

开发者 https://www.devze.com 2023-02-02 20:33 出处:网络
I need some help as i am just calling a method in a thread. now what i want is wait for reply form getData() method only for 15 seconds. If it reply before 15 seconds it should be terminated otherwise

I need some help as i am just calling a method in a thread. now what i want is wait for reply form getData() method only for 15 seconds. If it reply before 15 seconds it should be terminated otherwise after 15 seconds it should be terminated. The code is giv开发者_如何学JAVAen below:

boolean networkStatus;

private Runnable runnable; 

private ProgressDialog m_ProgressDialog = null;

        private Runnable returnRes = new Runnable() {

        @Override

        public void run() {

         if(networkStatus){

          setData();

         m_ProgressDialog.dismiss();

        }

    };

private void callGetdata(){

 runnable = new Runnable() {

        @Override

        public void run() { 

      networkStatus = getData();

             runOnUiThread(returnRes); 

        }

    };

    Thread thread = new Thread(null, runnable, "MovetoBackground");

    thread.start();

    m_ProgressDialog = ProgressDialog.show(this, "", getString(R.string.loadMsg), true);

}


What does getData() actually do, since some network classes have timeout options builtin, such as the ServerSocket.setSoTimeout() function.
Also, I would suggest using Android's AsyncTask class, as it makes multithreading easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消