Java 在android中限制对话框的范围
我正在从我的android应用程序向服务器打电话。我使用AsyncTask线程完成了这项工作。 现在,我在onPreExecute()方法中启动了进度对话框,如下所示:Java 在android中限制对话框的范围,java,android,android-asynctask,sharedpreferences,progressdialog,Java,Android,Android Asynctask,Sharedpreferences,Progressdialog,我正在从我的android应用程序向服务器打电话。我使用AsyncTask线程完成了这项工作。 现在,我在onPreExecute()方法中启动了进度对话框,如下所示: protected void onPreExecute(){ dialog = new ProgressDialog(appcontext); dialog.setMessage("Sending Position..."); dialog.setIndeterminate(true); dialog.setCancelabl
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
现在,在doInBackground()方法中,我一直在执行发送数据的任务。在得到响应后,我将使用对话框中的onPostExecute()方法。dismise()停止ProgressDialog
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
my doInBackGround()代码
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(Position... position){
InputStream inputStream = null;
String temp=null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2:8084/WebApplication5/getData");
String json = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.accumulate("time",position[0].getTime());
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content", "application/json");
org.apache.http.HttpResponse httpResponse = httpclient.execute(httpPost);
**// CODE ACTUALLY BLOCKS HERE UNTIL I GET A RESPONSE.(WHAT I THINKS). THOUGH WORKS 99%**
inputStream = httpResponse.getEntity().getContent();
if(inputStream!=null)
temp="sent";
}
catch (JSONException e){
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return temp;
}
一切都很好,但有时我没有得到响应(让它成为任何服务器错误),在这种情况下,对话框无法取消,并且正在不受限制地运行。我想将doInBackGround()方法的持续时间限制为几秒钟。因此,即使在没有响应的情况下,它也会移动到onPostExecute(),并且我的infinte Gong对话框应该停止
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
第二,如果我得到一个响应,我想把它保存到我的共享引用中,所以我的问题是什么是最好的方法,即
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
1) 我应该用onPause()方法来做
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
2) 或者,在onStop()方法中
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
3) 或者在onPostExecut()e方法中
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
或者这没有意义,我可以在任何地方做
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
我的最后一个问题是存储在共享首选项中的数据的生命周期是什么。您可以将其保存在需要的位置,也可以在获取后保存,或者保存在
onPause
中(活动暂停时调用该选项)
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
如果用户要修改它,而您需要保存它,请将其保存在onPause
中
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
或者,如果您只是需要它,并且不再更改它,请将其保存在onPostExecute
中,或者每次更改时都保存它
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
SharedReference
没有“生存期”,它将一直存在,直到应用程序在设备中,或者直到用户不清除数据应用程序
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
对于doBackground
,您可以在请求中添加超时
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
更改代码块:
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
HttpClient httpclient = new DefaultHttpClient();
到
protected void onPreExecute(){
dialog = new ProgressDialog(appcontext);
dialog.setMessage("Sending Position...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
Post ONPOST也执行代码。--你的背景将完成,你应该添加一些报告失败的东西。或者在postExecute中添加更多关于doBackground的信息我只是在使用dialog.discouse()并启动其他活动。如果出现问题,你的doBackground将永远运行吗?添加背景代码,这不正常。我已经编辑了问题。请再次查看..您可以添加一个超时:请给我应该调用setSoTimeOut()的对象名。谢谢。。顺便说一句:)