需要Android活动等待GPS位置获取

6
抱歉我的英文不好。 我试图从GPS获取单个位置,并将其放在全局变量latitude,longitude中。 GPS已启动,但在从GPS检索数据之前,活动继续进行。
换句话说,方法getCurrentLocation()必须仅在找到位置并填充经度和纬度变量后才能完成,以便我可以在其他方法中使用它们。 我知道...用户必须等待...我会通过在屏幕上显示一些东西来解决这个问题。 我该怎么办? 谢谢
我认为我在某个地方跳过了停止监听GPS。哪里更好?
以下是代码:
//Method to retrieve coordinates
public void getCurrentLocation() {
    //Use GPS if possible
    if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        //assign Listener to GPS
        manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.GPS_PROVIDER, Toast.LENGTH_SHORT).show();
    }
    else if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){//toherwise, use NETWORK
        //assign Listener to NETWORK
        manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
        Toast.makeText(this, LocationManager.NETWORK_PROVIDER, Toast.LENGTH_SHORT).show();
    }
}

//Class to store the location recived in two variables
final class MyLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        //coordinates storing
        latitude = String.valueOf(location.getLatitude());
        longitude = String.valueOf(location.getLongitude());
        Toast.makeText(getApplicationContext(), latitude + longitude, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
}
4个回答

2

使用LocusService第三方库!前往LocusService

通过调用简单的函数,您可以轻松地获取当前的GPS或网络位置....

要解决您的问题,请使用此代码!

LocusService locusService = new LocusService(this);
locusService.startRealtimeGPSListening(1000);   //Set intervel

locusService.setRealTimeLocationListener(new LocusService.RealtimeListenerService() {
        @Override
        public void OnRealLocationChanged(Location location) {
                if(location!=null){
                  //Do your Stuff here
                  finish();
                }
        }
    });

1
请注意,这只是 Android 位置监听器的简单包装器。 - daredesm

2
如果您想让Android活动等待GPS位置获取,可以尝试使用AsyncTask。请参见Android find GPS location once, show loading dialog中的答案。基本上,在onPreExecute中,您可以启动对话框(在调用doInBackground之前启动)。这意味着您要等到可以定位并显示对话框的时间。然后在doInBackground中,您可以获取位置。完成后将调用onPostExecute。您可以从onPostExecute内部停止它。如果需要,您还可以从onPostExecute内部调用其他函数,并检查位置是否不为空。

这可能是一种方法。您可以从AsyncTask Android example中学习一个基本示例。您也可以通过阅读此处的文档开始。

一些类似的有用问题:

Wait for current location - GPS - Android Dev

getting location instantly in android

希望这可以帮助您。


1

我遇到了同样的问题,

通过使用活动生命周期方法解决了它。


1) 在onCreate(Bundle b)方法中实现GPS捕获功能。

2) 在onStart()方法中需要GPS位置的意图或函数。


活动从onCreate(bundle)方法开始,此时将捕获GPS位置。在这里,GPS高度、纬度和经度值将被分配给全局变量。

之后将执行onStart()方法,在其中调用GPS位置全局变量以执行必要的操作。


希望这有所帮助。

1
最好使用getLastKnownLocation()方法。 以下是代码示例:
将您的纬度和经度作为全局变量。
double longitude;
double latitude;
protected void onCreate(Bundle savedBundle) {
       super.onCreate(savedBundle);
       setContentView(R.layout.activity_receipt_result);
       Objects.requireNonNull(getSupportActionBar()).setElevation(0);

       locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);

       if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    Activity#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for Activity#requestPermissions for more details.
        ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
    }else{
        Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Log.d("Location: ", "long-lat" + longitude + "-"+ latitude);
    }
}

在此之后,您可以在代码的其余部分使用经度和纬度变量。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接