handle 控制progressbar

zxiaocun 2012-12-17

发送一个请求执行progressbar加载条,直到服务器有响应就关闭progressbar

private void login(){

myHandler.sendEmptyMessage(1);

newThread(){

@Override

publicvoidrun(){

//TODOAuto-generatedmethodstub

super.run();

StringmobilePhone=et_mobile_phone.getText().toString();

Stringpwd=etPwd.getText().toString();

if(mobilePhone.length()<=0){

Toast.makeText(mainActivity.this,"请填写用户名",Toast.LENGTH_LONG).show();

return;

}

if(pwd.length()<=0){

Toast.makeText(mainActivity.this,"请填写密码",Toast.LENGTH_LONG).show();

return;

}

StringhttpGetString=null;

try{

httpGetString=httpClient.httpGet("login","?mobilePhone="+mobilePhone+"&password="+pwd);

}catch(ClientProtocolExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

ArrayList<String>getJson=null;

try{

getJson=httpClient.getResponse(httpGetString,"user");

}catch(JSONExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

if(getJson.get(0).equals("0")){

backCode=0;

Log.i("login", " "+getJson.get(1));

Log.i("login", " "+getJson.get(2));

backStr=getJson.get(2);

}

else{

backCode=-1;

backStr=getJson.get(1);

}

myHandler.sendEmptyMessage(0);

}

}.start();

}

Handler myHandler = new Handler() {

@Override

publicvoidhandleMessage(Messagemsg){

super.handleMessage(msg);

switch(msg.what){

case1:

progressBar.setVisibility(View.VISIBLE);

break;

case0:

progressBar.setVisibility(View.GONE);

if(backCode==-1){

Toast.makeText(mainActivity.this,backStr,Toast.LENGTH_SHORT).show();

}

else{

settings.edit().putString(KEY,backStr).commit();

intent=newIntent(mainActivity.this,orderActivity.class);

startActivity(intent);

}

break;

default:

break;

}

}

};

相关推荐

csuhanshuai / 0评论 2013-05-22
xiaozhifree / 0评论 2013-02-01