地理位置数据显示在我的模拟器上的应用程序中,但不在我的物理设备上

问题描述

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

这是在 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,"NULL");
                    }else{
                        Log.println(Log.VERBOSE,"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,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(),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,所以我不知道是什么问题

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...