我在主活动上有一个登录按钮,执行aysncTask,启动另一个活动onPostExecute,但是当用户多次点击按钮时 它多次启动活动,我该如何防止这种情况发生?
这是我的onClick Listener:
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (IP != "null") {
password = enterPassword.getText().toString();
progressB.setVisibility(View.VISIBLE);
new httpLogin().execute();}
else {
Toast.makeText(getApplicationContext(), "please enter H2O host address before attempting to login", Toast.LENGTH_LONG).show();
}
}
});
这是AsyncTask代码:
public class httpLogin extends AsyncTask<Void,Void,String> {
String rs,result;
JSONArray jArray;
boolean r,isConnected;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressB.setProgress(0);
isConnected=isOnline();
}
@Override
protected String doInBackground(Void... params) {
try {
String link = "http://"+IP+"/H2O.asmx/Check_Waiter?Password="+password;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
String[] separated = result.split(">");
JSONObject jObject = new JSONObject(separated[2]);
jArray = jObject.getJSONArray("Waiter");
if(jArray.length()>0) {
JSONObject json_data = jArray.getJSONObject(0);
waiterID = json_data.getString("UserNo");
waiterName = json_data.getString("UserName");
r=true;
}
else r=false;
rs = "sucessful";
} catch (Exception e) {
rs = "Fail";
}
return rs;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(isConnected)
if (r) {
Toast.makeText(getApplicationContext(), "User ID:" + waiterID + "\n Username:" + waiterName, Toast.LENGTH_SHORT).show();
Intent intentFSelector = new Intent(MainActivity.this, FloorSelector.class);
startActivity(intentFSelector);
}
}
这是ping方法,用于检查本地服务器是否可用:
public boolean isOnline() {
String[] separated = IP.split(":");
String hostip=separated[0];
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 "+hostip);
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
答案 0 :(得分:3)
执行AsyncTask
后禁用该按钮。
if (IP != "null") {
password = enterPassword.getText().toString();
progressB.setVisibility(View.VISIBLE);
new httpLogin().execute();
((Button) view).setEnabled(false);
}
理想情况下,您应该立即拨打startActivity()
按钮点击&amp;执行AsyncTask
中的onCreate()
。在下载数据时,您将在其中一个数据视图中显示“loading ...”。完成后,您将更新onPostExecute()
中的视图,或者如果下载失败则显示错误消息;说用户是否离线。
答案 1 :(得分:1)
试试这个并插入你的意图:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
答案 2 :(得分:1)
您可以使用ProgressDialog避免多次点击登录按钮。
ProgressDialog authProgressDialog;
在AsyncTask中
public class httpLogin extends AsyncTask<Void,Void,String> {
String rs,result;
JSONArray jArray;
boolean r,isConnected;
@Override
protected void onPreExecute() {
super.onPreExecute();
authProgressDialog = ProgressDialog.show(MainActivity.this, "", "Loading....", true, false);
}
protected Object doInBackground(Object... params) {
//your code
}
protected void onPostExecute(Object result){
if (authProgressDialog!=null) {
if (authProgressDialog.isShowing()) {
authProgressDialog.dismiss();
}
}
}
}
单击按钮时将显示进度对话框,执行AsyncTask后该对话框将关闭