0

I'm making this app, and when the user first opens the app, I want just a progress bar running until the app gets the user's GPS location. Then start the activity so they can do stuff. But I keep getting errors. I was wondering if someone can really help me out here?

public class MyActivity extends Activity implements View.OnClickListener {
    private ProgressBar pb = null;
    @InjectView(R.id.frame)
    private boolean testrun = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        while(!testRun)
        {
             setContentView(R.layout.ProgressBar);
             pb = (ProgressBar) findViewById(R.id.progressBar1);
             pb.setVisibility(View.VISIBLE);
             //Then I create an instance of AsyncTask and try to get GPS      
             //coordinates and set testRun to true. 

        }
        setContentView(R.layout.test);
        ButterKnife.inject(this);



    }

So I keep crashing, and it doesnt even load up the progress bar layout...

TheQ
  • 1,949
  • 10
  • 38
  • 63
  • Just set the visibility of the progress bar to gone once you have the location. No need for a while loop. Also, there are a number of things you should avoid in this code. – zgc7009 Aug 28 '15 at 21:27
  • @zgc7009 I just want to understand my error, like why does it crash when i put a while loop in with that, what's logical error? – TheQ Aug 28 '15 at 22:13
  • @TheQ The error might occur because you create the AsyncTask inside of the while loop and that it is therefore created like a thousand times or more. And also like I mentioned in my answer, you block the UI thread which will throw an error sooner or later. – jfmg Aug 28 '15 at 22:40

2 Answers2

2

You should not block the UI thread since the user won't be able to interact with your app anymore. Instead of the while loop, you should show a waiting indicator. For example you could show a spinning wheel and dismiss it as soon as you got the GPS position.

When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.

Source: http://developer.android.com/guide/components/processes-and-threads.html

Edit:
The LocationListener works with a callback. There is no need to put it in an AsyncTask. Just show some kind of waiting indicator and as soon as onLocationChanged is called you can dismiss the waiting indicator.

Community
  • 1
  • 1
jfmg
  • 2,626
  • 1
  • 24
  • 32
  • 1
    Oh I see, so blocking the UI thread anytime is a big no no. Thanks for the advice man, I'll do it a different way then. – TheQ Aug 28 '15 at 22:18
  • You got that right :-) Never block the UI thread. It doesn't matter if it's Android or another OS. Glad I could help. – jfmg Aug 28 '15 at 22:34
1

You do not need the while loop in the above snippet. This should give you an idea:

First see if the GPS is enabled on the device, else prompt the user to do so.

import android.location.LocationManager;
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            showGPSDisabledAlertToUser(); // Write your own code for this 
        }

Get the latitude and longitude(double values):

LocationResult locationResult = new LocationResult(){
            @Override
            public void gotLocation(Location location){
                //Got the location!
                latitude = location.getLatitude();
                longitude = location.getLongitude();
                 new sendRequest().execute();
                Log.i("","ALL" + latitude + ":" + longitude);
            }
        };
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(MainActivity.this, locationResult);

MyLocation.java file

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;

    public boolean getLocation(Context context, LocationResult result)
    {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 60000 * 15);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {

        }
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }
             locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

You need to modify the MyLocation file to remove redundancy based on your need, but this should give you the general idea.

Plus ensure this whole thing is in an AsyncTask and show() the ProgressDialog in the preexecute() and dismiss(), once the execution is complete in the postexecute()

Hope this helps.

Actiwitty
  • 1,232
  • 2
  • 12
  • 20
  • The LocationListener works with a callback. There is no need to put it in an AsyncTask. – jfmg Aug 28 '15 at 21:47
  • @FragmentsNActivities Yeah I'm familiar with that code, very much appreciated, but if I wanted to change my contentview temporarily until the GPS coordinates are found, would that be done in the getLocation method? – TheQ Aug 28 '15 at 21:58