Search code examples
androidandroid-activityandroid-asynctaskloading

how to show loading screen till the asynctask is done loading


Here is the code for the activity ,what creates the problem is the while loop it blocks the ui thread but it halts the conrol till the loading is done ,what i want is to show some image till the asynctask is done is task if success then screen1 else screen2.what currently i see is just black screen. Plz help.

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()); 

Solution

  • You do not need to track the current status of your AsyncTask. You can can identify when your AsyncTask is finished, When AsyncTask finished its task it will call the onPostExecute method.

    Your modified code

    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());