android处理吧问题

时间:2011-07-26 10:17:27

标签: android processing

我的程序从服务器检索一些数据。通常需要几秒钟。我想为此生成一个处理栏。而且我知道如何生成它。但是如何区分和控制何时关闭处理栏。 我的节目:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text1=(TextView)findViewById(R.id.textin);
        but1=(Button)findViewById(R.id.send);
        but2=(Button)findViewById(R.id.send2);
        edit1=(EditText)findViewById(R.id.textout);

        but1.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v) {
                Socket socket=null;
                String mesg=edit1.getText().toString()+"\r\n";
            try {
                socket=new Socket("localhost",30000);
                //send information to server
                PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
                out.println(mesg);
                //receive information from server
                BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String mstr=br.readLine();
            if(mstr!=null)
            {
                text1.setText(mstr);
            }else
            {
                text1.setText("error");
            }
                out.close();
                br.close();
                socket.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }catch(Exception e)
            {
                Log.e(DEBUG_TAG,e.toString());
            }
            }        
        });
      }

这是一个客户端程序。数据量非常大。那我怎么做处理吧。我该如何控制它? 谢谢!

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

如果您只是想告诉用户下载已完成,那么您可以使用Toast。在下载开始之前抛出消息“正在下载”,并在下载完成时禁用Toast消息。

假设您要显示已完成下载的百分比,那么您应该实现自己的进度条(类似于Media Player中的搜索栏)。继续根据已完成的百分比更新进度条(比如每秒)。

词shash

答案 2 :(得分:0)

您可以使用进度对话框。

以下是此代码:

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     ProgressDialog mProgressDialog;
      text1=(TextView)findViewById(R.id.textin);
     but1=(Button)findViewById(R.id.send);
     but2=(Button)findViewById(R.id.send2);
     edit1=(EditText)findViewById(R.id.textout);
      but1.setOnClickListener(new Button.OnClickListener()
     {
         public void onClick(View v) {
                public void onClick(View v) {
                mProgressDialog = ProgressDialog.show(Places.this,
            "Please wait...", "Connecting...", true, true, new DialogInterface.OnCancelListener() {public void onCancel(DialogInterface dialog) {
                }});

    new Thread() {
        public void run() {

             Socket socket=null;
             String mesg=edit1.getText().toString()+"\r\n";
         try {
             socket=new Socket("localhost",30000);
             //send information to server
             PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
             out.println(mesg);
             //receive information from server
             BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
             String mstr=br.readLine();
         if(mstr!=null)
         {
             text1.setText(mstr);
         }else
         { 
            text1.setText("error");
         }
             out.close();
             br.close();
             socket.close();
             sleep(3000);

         } catch (UnknownHostException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }catch(Exception e)
         {
             Log.e(DEBUG_TAG,e.toString());
         }
         mProgressDialog.dismiss();
        }                   

    }.start();
   } 
   }); 
                  }