安卓GPS超时GPS

2023-09-04 10:18:56 作者:烟火很不一般

编辑:我​​改写这个问题,因为我显然并不清楚

有时候,在Android手机上的GPS服务需要很长的时间去修复。有时,它的速度快,有时需要几个小时。我知道和接受这一点。

我有一个做了很多事情的应用程序。其中一件事,必须做的是允许用户点击一个按钮,其当前的坐标发送到服务器。我需要的是电话的坐标,当用户点击按钮的或在相当短的一段时间后。

因为我知道,得到一个GPS定位是不是瞬间,我知道,这可能需要几分钟或几小时(在此期间,用户移动很大的距离),我需要codeA超时此功能。针对这一特点,这是完全不能接受的上传用户三分钟的GPS位置(例如),他们单击该按钮后。这很好,如果需要45秒时,不会没关系,如果它需要75秒。它的优良给用户一个错误通知,如果该功能没有得到一个位置,速度不够快。

我需要一个功能获得GPS位置,并将其发送到服务器,,除非它需要超过一分钟

我原来的code是如下。因为张贴我已经改变了一些事情。我在onStartCommand()方法添加一个计时器。我开始一个TimerTask的是,60秒钟后会打电话给我的stop()方法。在onLocationChanged()方法的开头,我取消TimerTask的。

我的问题是:是定时方案实施这一超时的好办法?有没有更好的办法?

原题:

我正在写一个Android应用程序,除其他事项外,需要发送当前的GPS坐标到服务器,当用户告诉它。从上下文菜单中,我运行下面的服务。该服务是一个LocationListener的,并要求从LocationManager更新。当它到达一个位置(onLocationChanged()),它消除自身作为监听并发送的坐标关闭到服务器。所有这一切都正常工作。

不过,如果GPS坐标不赶紧用,我的服务只是继续工作,直到它得到了一些。它拥有了一个进度对话框,这是烦人的UI。更糟的是,如果用户已经自启动服务移动时,第一GPS坐标可能是错误的,该应用将不良数据发送到服务器。

我需要在服务超时。有没有好的办法做到这一点?我不是很有经验的线程。我想我可以运行一个Runnable在onStartCommand()方法,它会以某种方式倒计时30秒,然后,如果没有GPS的结果呢,叫我服务的stop()方法。这听起来像不像最好的方式做到这一点?

另外,是否有可能告诉我们,如果GPS不能得到解决?我怎么会去这样做?

编辑:要进一步澄清,我正在寻找在一定的时间后,得到一个位置放弃的最好办法

 公共类AddCurrentLocation扩展服务实现LocationListener的{

    应用程序;
    LocationManager mLocManager;
    ProgressDialog mDialog;

    @覆盖
    公众诠释onStartCommand(意向意图,诠释为arg0,诠释ARG1){
        应用= getApplication();

        //显示进度对话框
        如果(app.getScreen()!= NULL){
            mDialog = ProgressDialog.show(app.getScreen(),,添加位置请稍候......,真正的);
        }

        //寻找GPS服务,并开始收听
        标准标准=新标准();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        mLocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        字符串bestProvider = mLocManager.getBestProvider(标准,真正的);
        mLocManager.requestLocationUpdates(bestProvider,2000,0,这一点);

        返回START_NOT_STICKY;
    }

    私人无效停止(){
        mLocManager.removeUpdates(本);
        如果(mDialog!= NULL){
            mDialog.dismiss();
        }
        stopSelf();
    }

    @覆盖
    公共无效onLocationChanged(位置定位){
        //与GPS停止收听完成
        mLocManager.removeUpdates(本);

        sendLocation(位置); //方法发送信息到服务器
        停止();
    }

    //其他所需的方法和sendLocation()...

}
 

解决方案

这不是真的它是如何工作。它会持续那么长的时间,在大多数情况下,以获得GPS定位。但是,从对每个更新该点(每2秒在code)的是人的当前位置。而你得到的将是人的当前位置,所以数据不会过时的首次定位。

另一件事。如果你是在一个服务运行此code,你不应该阻止用户界面有一个进度对话框,绝对不是从服务。这是一个内存泄漏等情况发生。您应该只出现进展,如果它是什么,可能需要5秒,最多,在活动线程运行。另一种选择是显示进度在标题栏对话框,并且仍然让用户与应用程序交互(这就是为什么你使用的服务反正)。显示进步为很长一段时间真的不是那么人性化。尤其是当他们以某种方式改变方向(也许事故),然后你的应用程序崩溃,因为该服务句柄对话,他们不得不重新开始。

看看的谷歌I / O 2010应用看到如何将一个很好的例子活动应该与一个服务。它使用了一个服务拉回来数据,并显示在标题的进度而服务正在做一些工作。而仍然让你做其他事情的应用程序。

Edit: I'm rewriting this question because I apparently wasn't clear.

安卓平板gps价格 安卓平板gps批发 安卓平板gps厂家

Sometimes the GPS service on Android phones takes a long time to get a fix. Sometimes it's fast, sometimes it takes hours. I know and accept this.

I have an application that does many things. One of the things it must do is allow the user to click a button to send their current coordinates to a server. What I need are the coordinates of the phone when the user clicks the button or within a reasonably short time thereafter.

Because I know that getting a GPS fix is not instant and I know that it could take minutes or hours (during which the user has moved a great distance), I need to code a timeout for this feature. For this feature, it is simply not acceptable to upload the GPS location of the user three minutes (for example) after they clicked the button. It's fine if it takes 45 seconds, not okay if it takes 75 seconds. It's fine to give the user an error notification if the feature failed to get a location fast enough.

I need a feature to 'get the GPS location and send it to the server, unless it takes more than one minute'.

My original code is below. I have changed some things since posting it. I have added a Timer in the onStartCommand() method. I start a TimerTask that after 60 seconds will call my stop() method. At the beginning of the onLocationChanged() method, I cancel the TimerTask.

My question is: Is the Timer scheme a good way of implementing this timeout? Is there a better way?

Original question:

I'm writing an Android application that, among other things, needs to send the current GPS coordinates to a server when the user tells it to. From a context menu, I run the service below. The service is a LocationListener and requests updates from the LocationManager. When it gets a location (onLocationChanged()), it removes itself as a listener and sends the coordinates off to the server. All of this is working.

However, if GPS coordinates are not quickly available, my service just keeps running until it gets some. It holds up the UI with a progress dialog, which is annoying. Worse, if the user has moved since starting the service, the first GPS coordinates might be wrong and the app will send bad data to the server.

I need a timeout on the service. Is there a good way to do that? I'm not very experienced with threads. I think I can run a Runnable in the onStartCommand() method that will somehow count down 30 seconds and then, if there is no GPS result yet, call my service's stop() method. Does that sound like the best way to do this?

Alternatively, is it possible to tell if the GPS cannot get a fix? How would I go about doing that?

Edit: To further clarify, I'm looking for the best way to "give up" on getting a Location after some amount of time.

public class AddCurrentLocation extends Service implements LocationListener {

    Application app;
    LocationManager mLocManager;
    ProgressDialog mDialog;

    @Override
    public int onStartCommand(Intent intent, int arg0, int arg1) {
        app = getApplication();

        // show progress dialog
        if (app.getScreen() != null) {
            mDialog = ProgressDialog.show(app.getScreen(), "", "Adding Location. Please wait...", true);
        }

        // find GPS service and start listening
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String bestProvider = mLocManager.getBestProvider(criteria, true);
        mLocManager.requestLocationUpdates(bestProvider, 2000, 0, this);

        return START_NOT_STICKY;
    }

    private void stop() {
        mLocManager.removeUpdates(this);
        if (mDialog != null) {
            mDialog.dismiss();
        }
        stopSelf();
    }

    @Override
    public void onLocationChanged(Location location) {
        // done with GPS stop listening
        mLocManager.removeUpdates(this);

        sendLocation(location); // method to send info to server
        stop();
    }

    // other required methods and sendLocation() ...

}

解决方案

That's not really how it works. It will consistently take that long in most situations to get a GPS fix. But from that point on each update (every 2 sec in your code)will be the person's current position. And the first fix you get will be the person's current position, so the data will not be "out of date".

Another thing. If you are running this code in a service you shouldn't block the UI with a progress dialog and definitely not from the Service. That is a memory leak waiting to happen. You should only show progress if it is something that might take 5 sec at the most and is running in a thread in the Activity. Another option is to show the progress dialog in the title bar, and still let the user interact with the app (which is why you use a service anyway). Showing progresses for a long period of time really isn't that User Friendly. Especially if they somehow change orientation (maybe on accident) and then your app crashes because of the service handle to the dialog and they have to start over.

Take a look at the Google I/O 2010 app to see a great example of how an activity should work with a service. It uses a service to pull back data, and shows a progress in the title while the service is doing some work. And still lets you do other things in the app.