【发布时间】:2015-11-02 12:55:17
【问题描述】:
我试图在我的登录类中显示是否有互联网连接。还有另一个连接管理器类,如果没有互联网连接,这个类会显示一个警报对话框,但我对在哪里实现代码感到困惑,因为 Login 类中还有一个异步任务(尝试登录)谁能告诉我在哪里放置我的代码以及我哪里出错了?
public class LoginActivity extends Activity implements OnClickListener {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
EditText username, password;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
/**
* Check Internet status button click event
* */
username = (EditText) findViewById(R.id.username_et);
password = (EditText) findViewById(R.id.password_et);
login = (Button) findViewById(R.id.login_bt);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
showAlertDialog(LoginActivity.this, "Internet Connection",
"You have internet connection", true);
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(LoginActivity.this, "No Internet Connection",
"You don't have internet connection.", false);
}
String name = username.getText().toString();
String pass = password.getText().toString();
new AttemptLogin().execute(name, pass);
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
});
}
private class AttemptLogin extends
AsyncTask<String, Integer, String> {
int success;
String message = " ", _username, _password;
@Override
protected String doInBackground(String... args) {
_username = args[0];
_password = args[1];
try {
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "login");
params.put("username", _username);
params.put("password", _password);
HttpUtility.sendPostRequest(params);
String response = HttpUtility.readRespone();
JSONObject jObj = null;
try {
jObj = new JSONObject(response);
success = jObj.getInt("success");
message = jObj.getString("message");
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data" + e.toString());
}
} catch (IOException ex) {
ex.printStackTrace();
}
HttpUtility.disconnect();
return message;
}
protected void onPostExecute(String status) {
if (status != null) {
Toast.makeText(getBaseContext(), status, Toast.LENGTH_LONG).show();
if (success == 1) {
SharedPreference.store(getApplicationContext(), _username, _password);
startActivity(new Intent(getBaseContext(), DashboardActivity.class));
}
}
}
}
}
【问题讨论】:
-
可以在aysnc任务的post execute方法中显示alertdialog
-
In ok Button onClick of
showAlertDialog我认为是开始AsyncTask任务的正确位置 -
所以我把这段代码的 showAlertDialog 部分放在 AsyncTask 中,其余的留在 Login 类中?
-
IMO,alertdialog 太过分了——你可以使用
Toast toast = Toast.makeText(context, text, duration).show(); -
不幸的是我需要使用警报对话框:(
标签: java android android-asynctask connection