getlastknownlocation第一次返回null问题

fykwrbwg  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(275)

我正在开发一个简单的android定位应用程序,但是我有一点小麻烦(也很抱歉我的英语不好)
我想通过单击按钮获取当前位置,但getlastknownlocation第一次返回null。
但是,如果我首先打开谷歌Map,在Map上显示我的当前位置,然后我通过后台,然后我打开自己的应用程序,点击按钮,工作。但我不想这样,我只想在我的应用程序上提供位置不是这样。

@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();
    }

}

}

没有空值如何获取位置?

zmeyuzjn

zmeyuzjn1#

这不是一个bug,只是谷歌的政治问题。getlastknowlocation()方法返回google服务检索到的最后一个位置。正如谷歌文档所说:
在以下情况下,location对象可能为空:
设备设置中的位置已关闭。即使以前检索过最后一个位置,结果也可能为空,因为
禁用位置也会清除缓存。
设备从未记录其位置,可能是新设备或已恢复出厂设置的设备。
设备上的google play服务已重新启动,并且没有活动的fused location provider客户端在服务重新启动后请求位置。
这就是为什么如果你从谷歌Map切换到你的应用程序,你可以得到最后的位置。要获得实际位置而不需要从一个应用程序传递到另一个应用程序,您需要实现一个新的客户端并自己请求位置更新。有关详细信息,请参阅接收位置更新。
我希望我已经澄清了你的每一个疑问。否则,请毫不犹豫地写:)

相关问题