简体   繁体   English

Android:让应用程序等到找到当前位置

[英]Android : Make application wait till the current location is found

I'm trying to make my app wait till the current location is found. 我正在尝试让我的应用程序等到找到当前位置。 I've tried few different ways using Threads and all have failed really. 我尝试过使用Threads几种不同方式,但都失败了。 I was using wait() and notify() but application just hung and never found the current location. 我正在使用wait()notify()但是应用程序只是挂起并且从未找到当前位置。

I amen't using google map api as it is not part of the application. 我不使用谷歌地图api,因为它不是应用程序的一部分。 Does anyone have any ideas how to do this or examples. 有没有人有任何想法如何做到这一点或例子。

EDIT : The Thread I was using did not start till the user pressed a button then within onLocationChanged other data is processed eg adding the new location to ArrayList, Calculate the distance between the current and last Location as well as the Time taken to get to the new location 编辑:我使用的Thread没有启动,直到用户按下按钮然后在onLocationChanged处理其他数据,例如将新位置添加到ArrayList,计算当前和最后一个位置之间的距离以及到达新的位置

You could try starting an AsyncTask in onCreate to get the location. 您可以尝试在onCreate启动AsyncTask来获取位置。 Your default onCreate layout could be a "loading" page, then when your AsyncTask completes successfully with the location it draws your "real" UI. 您的默认onCreate布局可能是一个“加载”页面,然后当您的AsyncTask成功完成该位置时,它会绘制您的“真实”UI。

So if I understand what you want to do correctly, then I would avoid making another thread in onClick() . 所以,如果我理解你想要正确做什么,那么我会避免在onClick()创建另一个线程。 Instead, onClick() should just request a location, display a progress dialog, and return. 相反, onClick()应该只是请求一个位置,显示一个进度对话框,然后返回。 Since the work you want to do happens after you receive the new location, I would start an AsyncTask there. 由于你想要做的工作是在你收到新位置后发生的,我会在那里开始一个AsyncTask。 Then you finally remove the dialog box (removing it returns control to the user) when the AsyncTask finishes. 然后,当AsyncTask完成时,最后删除对话框(删除它将控件返回给用户)。

Code usually helps, so, I would put this in onCreate() or wherever: 代码通常有帮助,所以,我会把它放在onCreate()或任何地方:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        listener.refresh();
    }
});

And put this in your LocationListener: 并将其放在LocationListener中:

public void refresh() {
    myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    myDialog = new ProgressDialog(myContext);
    myDialog.setIndeterminate(true);
    myDialog.show();
}

@Override
public void onLocationChanged(Location location) {
    // now do work with your location, 
    // which your probably want to do in a different thread
    new MyAsyncTask().execute(new Location[] { location });
}

And then you need an AsyncTask, which may look like this: 然后你需要一个AsyncTask,它可能看起来像这样:

class MyAsyncTask extends AsyncTask<Location, Void, Void> {
    @Override
    protected Void doInBackground(Location... location) {
        // start doing your distance/directions/etc work here
        return null;
    }


    @Override
    protected void onPostExecute(Void v) {
        // this gets called automatically when you're done, 
        // so release the dialog box
        myDialog.dismiss();
        myDialog = null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM