android processing bar problem
My programme retrieves some data from server. Usually it takes few seconds. I want to generate a processing bar for this. And I know how to generate it. But how to distinguish and control when close processing bar. My programme:
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());
}
}
});
}
This a client program. The amount of data is really huge. So how can I do the processing bar. How can I CONTROL it? Thank!!!
http://sites.google.com/site/androidhowto/how-to-1/create-a-custom-progress-bar-using-asynctask
Here is an awsome tutorial on progress bars.
You can use Progress Dialog for this.
Here is this code:
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();
}
});
}
If you just want to tell user that downloading has been completed, then you can use Toast. Throw the message "Downloading" just before downloading starts and disable Toast message on download completion.
Suppose you want to show the % of completed download, then you should implement your own progress bar (Similar to seek bar in Media Player). Keep on updating progress bar based on % completed (say per second basis).
Shash
精彩评论