开发者

Displaying progress dialog at the start of a GPS application

I am developing a app in which I want the progress dialog to be displayed when the app is starte开发者_C百科d (when the GPS service starts). I want to end the progress dialog when the GPS service returns with a valid position. I know I have to start another thread but I am not sure how to do it. I have:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                1000, 10, this);

in my onCreate().

public void onLocationChanged(Location location) is where the data is being updated.


I assume that the real problem you have is that you don't know when you will actually get a valid position, in that case displaying a progress bar would be difficult to do since there is no way for you to determine how far along are you.

I would recommend that you show a message the user that says "Acquiring GPS Position" and possibly have some type of animation with a GPS satellite spinning, a "signal" traveling between a GPS satellite and a GPS receiver, or a flashing GPS icon indicating that you're acquiring a GPS signal (but make it more pleasant to look at):

Displaying progress dialog at the start of a GPS application

Displaying progress dialog at the start of a GPS application

Displaying progress dialog at the start of a GPS application

So in your GUI thread you will render the animation and you will simultaneously run another thread that will acquire the GPS signal. When you acquire the GPS signal you will terminate the rendering:

To start the new thread you do this:

class GPSPositionGetter extends Runnable
{
    private LocationManager _locationManager;
    GPSPositionGetter(LocationManager locationManager)
    public run()
    {
         _locationManager.requestLocationUpdates(...);
    }
}

Thread t = new Thread(new GPSPositionGetter(locationManager));
t.start();

In your GUI thread:

while(!positionAcquired)
{
    renderAnimation();
}

Inside the callback method you do:

public onLocationChanged(Location location) 
{
    if(IsValid(location))
    {
        positionAcquired = true;
    }
}

Note that you should declare positionAcquired as a volatile in order to ensure visibility and to prevent it from being cashed in a thread's local memory.

P.S. the code is just to give you a rough idea of what to do; there are other more elegant solutions (such as using an ExecutorService), but the general approach should not be much different.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜