getLastKnownLocation第一次返回null问题

时间:2019-02-04 17:55:30

标签: java android location-services

我正在开发一个简单的android定位应用,但是有点麻烦。(也为我的英语不好而感到抱歉。)

我想通过单击按钮来获取当前位置,但是getLastKnownLocation第一次都返回null。

但是,如果我首先打开google maps并在地图上显示我的当前位置,那么在通过背景设置之后,我将打开自己的应用程序,然后单击有效的按钮。但是我不希望这样,我只是不想在我的应用程序上提供位置。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Object clipboardService = getSystemService(CLIPBOARD_SERVICE);
    final ClipboardManager clipboardManager = (ClipboardManager) clipboardService;
    tvEnlem = (TextView) findViewById(R.id.textViewEnlem);
    tvBoylam = (TextView) findViewById(R.id.textViewBoylam);
    retrieveLocationButton = (Button) findViewById(R.id.buttonNeredeyim);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
        //    ActivityCompat#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 ActivityCompat#requestPermissions for more details.
        return;
    }


    retrieveLocationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showCurrentLocation();
        }
    });

}

protected void showCurrentLocation() {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 101);
        //    ActivityCompat#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 ActivityCompat#requestPermissions for more details.

        return;
    }


    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();

        this.tvBoylam.setText(String.valueOf(location.getLongitude()));
        this.tvEnlem.setText(String.valueOf(location.getLatitude()));

    }
    else{

        Toast.makeText(MainActivity.this,"LOKASYON BİLGİLERİ BOŞ",
                Toast.LENGTH_SHORT).show();

    }


}

private class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location location) {
        String message = String.format(
                "New Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(MainActivity.this, "Provider status changed",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(MainActivity.this,
                "Provider disabled by the user. GPS turned off",
                Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        Toast.makeText(MainActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}



}

如何获取没有空值的位置?

1 个答案:

答案 0 :(得分:0)

这不是错误,而只是Google政治。 getLastKnowLocation()方法返回google服务检索到的最后一个位置。 正如Google文档所说:

  

在以下情况下,定位对象可能为null:

     
      
  • 在设备设置中关闭了位置。即使先前检索了最后一个位置,结果也可能为null,因为
      禁用位置也会清除缓存。
  •   
  • 该设备从未记录其位置,这可能是新设备或已恢复出厂的设备的情况   设置。
  •   
  • 设备上的Google Play服务已重新启动,并且没有活动的融合位置提供程序客户端请求   服务重启后的位置。
  •   

这就是为什么如果您从Google Maps切换到您的应用程序,则可以获得上一个位置的原因。 要获得实际位置而不通过应用程序传递给其他应用程序,您需要实现一个新客户端并要求自己更新位置。有关更多信息,请参见Receiving Location Updates

希望我能澄清您的每一个疑问。否则,请毫不犹豫地写:)