0

我的应用程序包含我现在用作虚拟对象的活动。单击按钮时,屏幕上会显示经度和纬度等地理位置。IT 在模拟器上运行良好,但在设备上运行良好。我尝试在设备上打开和关闭位置查找器,但没有任何反应。当我卸载并重新安装它时,它会请求允许找到我的位置,但就是这样,什么都没有显示。

这是在 onCreate 方法内部:

locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(1000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(locationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();

    locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(@NonNull LocationResult locationResult) {
            super.onLocationResult(locationResult);
            if (locationResult == null){
                Log.println(Log.VERBOSE,"LocationResult","NULL");
            }else {
                for(Location location : locationResult.getLocations()){
                    if (location == null){
                        Log.println(Log.VERBOSE,"LocationResult","NULL");
                    }else{
                        Log.println(Log.VERBOSE,"LocationResult","found location: "+location.getLatitude()+","+location.getLongitude());
                        //Initialize geocoder
                        Geocoder geocoder = new Geocoder(get_location.this, Locale.getDefault());
                        //Initialize address list
                        try {
                            List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                            textView_gps_loc.setText("Latitude: " + addresses.get(0).getLatitude()
                                    + "\nLongitude: " + addresses.get(0).getLongitude()
                                    + "\nCountry: " + addresses.get(0).getCountryName()
                                    + "\nLocality: " + addresses.get(0).getLocality()
                                    + "\nAddress Line" + addresses.get(0).getAddressLine(0));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    };

然后外面是这样的:

private void getLocation() {
    Log.println(Log.VERBOSE,"Location","getting location");

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    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.
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(get_location.this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
            new AlertDialog.Builder(get_location.this)
                    .setTitle(R.string.title_location_permission)
                    .setMessage(R.string.text_location_permission)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //Prompt the user once explanation has been shown
                            ActivityCompat.requestPermissions(get_location.this,
                                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                    MY_PERMISSIONS_REQUEST_LOCATION);
                        }
                    })
                    .create()
                    .show();


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(get_location.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return;
    }
    else {
        fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                //Initialize Location
                Location location = task.getResult();
                if (location != null) {
                    //Initialize geocoder
                    Geocoder geocoder = new Geocoder(get_location.this, Locale.getDefault());
                    //Initialize address list
                    try {
                        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                        textView_gps_loc.setText("Latitude: " + addresses.get(0).getLatitude()
                                + "\nLongitude: " + addresses.get(0).getLongitude()
                                + "\nCountry: " + addresses.get(0).getCountryName()
                                + "\nLocality: " + addresses.get(0).getLocality()
                                + "\nAddress Line" + addresses.get(0).getAddressLine(0));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
    }
}

我的可穿戴设备支持 GPS,所以我不知道是什么问题

4

0 回答 0