如何在异步任务完成加载之前显示加载屏幕
how to show loading screen till the asynctask is done loading
这是 activity 的代码,造成问题的是 while 循环它阻塞了 ui 线程,但它会停止控制直到加载完成,我想要的是显示一些图像直到 asynctask 完成是任务,如果成功则屏幕 1 否则 screen2.what 目前我看到的只是黑屏。
请帮助。
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());
您似乎没有获取 AsyncTasks。它们并行执行。因此,您不要在调用执行后放置要在其完成后执行的代码。您将它放在 onPostExecute 函数中,该函数在任务完成后在 UI 线程上调用。您永远不会循环、休眠或以其他方式等到 AsyncTask 完成 - 这与整点背道而驰。
您不需要跟踪 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());
这是 activity 的代码,造成问题的是 while 循环它阻塞了 ui 线程,但它会停止控制直到加载完成,我想要的是显示一些图像直到 asynctask 完成是任务,如果成功则屏幕 1 否则 screen2.what 目前我看到的只是黑屏。 请帮助。
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());
您似乎没有获取 AsyncTasks。它们并行执行。因此,您不要在调用执行后放置要在其完成后执行的代码。您将它放在 onPostExecute 函数中,该函数在任务完成后在 UI 线程上调用。您永远不会循环、休眠或以其他方式等到 AsyncTask 完成 - 这与整点背道而驰。
您不需要跟踪 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());