Displaying progress dialog at the start of a GPS application

橙三吉。 提交于 2019-12-11 10:28:11

问题


I am developing a app in which I want the progress dialog to be displayed when the app is started (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.


回答1:


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):

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.



来源:https://stackoverflow.com/questions/6958695/displaying-progress-dialog-at-the-start-of-a-gps-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!