• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How do I wait for LocationClient to connect?

oneeyeman

Lurker
Hi, ALL,
I am trying to implement the GooglePlayServices in order to get the location of the phone.
I followed the official Android tutorial and now am stuck.

Here's my code:

Code:
class MyActivity extends Activity implements  GooglePlayServicesClient.ConnectionCallbacks,  GooglePlayServicesClient.OnConnectionFailedListener
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate( savedInstanceState );
        context = this;
        if( servicesConnected() )
        {
            client = new LocationClient( this, this, this );
            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();
            device = (Device) bundle.get( "device" );
        }
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        if( client != null )
        {
            client.connect();
            UserListAdapter adapter = new UserListAdapter( this, R.layout.grid_user, friends );
            setContentView( R.layout.main_screen );
            GridView view = (GridView) findViewById( R.id.view_users );
            view.setAdapter( adapter );
        }
    }

    @Override
    protected void onStop()
    {
        client.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(Bundle arg0)
    {
        AsyncTask<Void,Void,ArrayList<Device>> task = new AsyncTask<Void,Void,ArrayList<Device>>()
        {
            @Override
            protected void onPreExecute()
            {
                pd = new ProgressDialog( context );
                pd.setMessage( "Retrieving nearby friends..." );
                pd.setCancelable( false );
                pd.setIndeterminate( true );
                pd.show();
            }
            
            @Override
            protected ArrayList<Device> doInBackground(Void... params)
            {
                currLocation = client.getLastLocation();
                device.setLatitude( currLocation.getLatitude() );
                device.setLongtitude( currLocation.getLongitude() );
                DBCommunicator db = new DBCommunicator( getApplicationContext() );
                try
                {
                    friends = db.updateLocation( device );
                }
                catch (ParseException e)
                {
                    if( pd != null )
                    {
                        pd.dismiss();
                        pd = null;
                    }
                }
                return friends;
            }
            
            @Override
            protected void onPostExecute(ArrayList<Device> result)
            {
                if( pd != null )
                    pd.dismiss();
            }
        };
        task.execute( (Void[]) null );
    }

}
The problem I'm facing is that right now I'm calling "client.connect()", but this call is synchronous and when I later trying to use the object that needs to be made after successful connection and retrieval of the location it is NULL.

I tried to wrap the call to "client.connect()" in an AsyncTask<Void,Void,Void>(), but unfortunately, this is also didn't help.

So my question is: what is proper way to wait for connection and retrieving the data from cloud, so that the UI won't crash and its thread won't be stopped?

Thank you.

[EDIT]
Basically I'm looking for the same algorithm as in the Navigator.
Presumably it starts a thread where it asks for a location and then calculates the route but the map is not fully drawn until after the calculation is finished and the location/route are established.
[/EDIT]
 
Back
Top Bottom