这是活动的代码,创建问题的是while循环它阻塞ui线程但它停止控制直到加载完成,我想要的是显示一些图像,直到asynctask完成是任务如果成功那么screen1 else screen2.what我目前看到的只是黑屏。 Plz帮助。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Spinner;
public class loadingscreen extends Activity {
public final static String UID_MESSAGE="abe user name hai be";
int al=0,rt=0;
public static Spinner pb;
SharedPreferences save;
TelephonyManager tel;
SharedPreferences.Editor editor;
networker create;
String def=null;
String newt=null;
String imei=null;
String bus="AA";
Context context;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
save=getSharedPreferences("map",MODE_PRIVATE);
editor=save.edit();
tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
setContentView(R.layout.loading);
pb=(Spinner)findViewById(R.id.pb);
def=save.getString("uid","test");
imei=tel.getDeviceId();
if(!(def.equals("true"))){
Log.e(null," please wait logging in ");
new fsnetworker(context,0,0,0,5).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imei,def,bus);
while(fsnetworker.done==0){ Log.e(null,"trying "); }
if(fsnetworker.done==4 ){
intent = new Intent(getBaseContext(),MapActivity.class);
intent.putExtra(UID_MESSAGE, def);
startActivity(intent);
}
}
intent = new Intent(getBaseContext(),firstScreen.class);
intent.putExtra(UID_MESSAGE, def);
startActivity(intent);
}
public static class fsnetworker extends AsyncTask<String,Void,String>{
Context context;
double lat=0f;
double lng=0f;
float acc=0;
int rt=0;
String imei;
String uid;
String bus;
public static int done;
public fsnetworker(Context context,double lat2,double lng2,float acc2,int rt){
this.context=context;
this.lat=lat2;
this.lng=lng2;
this.acc=acc2;
this.rt=rt;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String success) {
super.onPostExecute(success);
}
@Override
protected String doInBackground(String... arg0) {
try{
done=0;
Log.e(null,"networking");
imei=(String)arg0[0];
uid=(String)arg0[1];
bus=(String)arg0[2];
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet();
get.setURI(new URI("http://*********************?"
+"imei="+imei
+"&name="+uid
+"&lat="+lat
+"&lng="+lng
+"&acc="+acc
+"&bus="+bus
+"&rt="+rt));
HttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
BufferedReader sread=new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb = new StringBuilder();
String out=null;
while((out=sread.readLine())!=null){
sb.append(out);
break;
}
Log.e(" url", ""+sb.toString());
答案 0 :(得分:1)
您似乎没有获得AsyncTasks。它们并行执行。因此,在调用execute之后,不要放置要执行的代码。您将它放在onPostExecute函数中,该函数在任务完成后在UI线程上调用。你永远不会循环,睡眠或以其他方式等待AsyncTask完成 - 这与整个点相对。
答案 1 :(得分:0)
您无需跟踪AsyncTask的当前状态。您可以识别AsyncTask何时完成,当AsyncTask完成其任务时,它将调用onPostExecute方法。
您修改后的代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Spinner;
public class loadingscreen extends Activity {
public final static String UID_MESSAGE="abe user name hai be";
int al=0,rt=0;
public static Spinner pb;
SharedPreferences save;
TelephonyManager tel;
SharedPreferences.Editor editor;
networker create;
String def=null;
String newt=null;
String imei=null;
String bus="AA";
Context context;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
save=getSharedPreferences("map",MODE_PRIVATE);
editor=save.edit();
tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
setContentView(R.layout.loading);
pb=(Spinner)findViewById(R.id.pb);
def=save.getString("uid","test");
imei=tel.getDeviceId();
if(!(def.equals("true"))){
Log.e(null," please wait logging in ");
new fsnetworker(context,0,0,0,5).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,imei,def,bus);
}
intent = new Intent(getBaseContext(),firstScreen.class);
intent.putExtra(UID_MESSAGE, def);
startActivity(intent);
}
public static class fsnetworker extends AsyncTask<String,Void,String>{
Context context;
double lat=0f;
double lng=0f;
float acc=0;
int rt=0;
String imei;
String uid;
String bus;
public static int done;
public fsnetworker(Context context,double lat2,double lng2,float acc2,int rt){
this.context=context;
this.lat=lat2;
this.lng=lng2;
this.acc=acc2;
this.rt=rt;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String success) {
super.onPostExecute(success);
intent = new Intent(getBaseContext(),MapActivity.class);
intent.putExtra(UID_MESSAGE, def);
startActivity(intent);
}
@Override
protected String doInBackground(String... arg0) {
try{
done=0;
Log.e(null,"networking");
imei=(String)arg0[0];
uid=(String)arg0[1];
bus=(String)arg0[2];
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet();
get.setURI(new URI("http://*********************?"
+"imei="+imei
+"&name="+uid
+"&lat="+lat
+"&lng="+lng
+"&acc="+acc
+"&bus="+bus
+"&rt="+rt));
HttpResponse response=client.execute(get);
HttpEntity entity=response.getEntity();
BufferedReader sread=new BufferedReader(new InputStreamReader(entity.getContent()));
StringBuilder sb = new StringBuilder();
String out=null;
while((out=sread.readLine())!=null){
sb.append(out);
break;
}
Log.e(" url", ""+sb.toString());
&#13;