【问题标题】:Need android activity to wait until GPS location obtained需要 android 活动才能等到获得 GPS 位置
【发布时间】:2013-07-25 16:30:49
【问题描述】:

对不起我的英语。 我正在尝试从 GPS 获取单个位置以放置全局变量纬度、经度。 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
    }
}

【问题讨论】:

    标签: android android-activity gps wait android-location


    【解决方案1】:

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

    这可能是一种方式。您可以从AsyncTask Android example 学习一个基本示例。您也可以先阅读文档here

    其他一些类似的有用问题:

    Wait for current location - GPS - Android Dev

    getting location instantly in android

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      使用 LocusService 3rd 方库! Goto 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();
                      }
              }
          });
      

      【讨论】:

      • 请注意,这只是 android 位置监听器的简单包装。
      【解决方案3】:

      我遇到了同样的问题,

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


      1)onCreate(Bundle b)方法内的GPS捕捉功能

      2) 需要 GPS 定位的意图或功能,在onStart() 方法中。


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

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


      希望对你有帮助

      【讨论】:

        【解决方案4】:

        最好使用 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);
            }
        }
        

        在您可以在其余代码中使用经度和纬度变量之后。

        【讨论】:

          猜你喜欢
          • 2015-04-09
          • 2010-11-25
          • 1970-01-01
          • 1970-01-01
          • 2012-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多