在GPS应用程序开始时显示进度对话框 [英] Displaying progress dialog at the start of a GPS application

查看:69
本文介绍了在GPS应用程序开始时显示进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,希望在该应用程序启动时(GPS服务启动时)显示进度对话框.我想在GPS服务返回有效位置时结束进度对话框. 我知道我必须启动另一个线程,但是我不确定该怎么做. 我有:

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

在我的onCreate()中.

public void onLocationChanged(Location location)是要更新数据的位置.

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.

我建议您向用户显示一条消息,提示用户正在获取GPS位置",并且可能带有某些类型的动画,其中包括GPS卫星旋转,信号"在GPS卫星和GPS接收器之间传播或闪烁表示您正在获取GPS信号的GPS图标(但看起来更舒服):

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

因此,在GUI线程中,您将渲染动画,同时运行另一个线程,该线程将获取GPS信号.当您获取GPS信号时,您将终止渲染:

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

在您的GUI线程中:

while(!positionAcquired)
{
    renderAnimation();
}

在回调方法内部:

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

请注意,您应将positionAcquired声明为volatile,以确保可见性并防止其在线程的本地内存中兑现.

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.该代码只是为了让您大致了解要做什么;还有其他更优雅的解决方案(例如使用ExecutorService),但是一般方法应该没有太大不同.

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.

这篇关于在GPS应用程序开始时显示进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆