使用原生网络定位,部分机型无法获取经纬度该怎么解决?

现在是小米,华为,vivo可以,锤子坚果pro2不行,下面是代码:

public class FinishedContractActivity extends BaseActivity implements LocationListener {

    private BottomSheetDialog mCheckSignDialog;
    private BottomSheetDialog mSignProcessDialog;
    private CheckSignBean checkSignBean;
    private SignProcessBean signProcessBean;
    private SealPresenter mPresenter = new SealPresenter(this);
    private LocationManager locationManager;

    //省略部分无关代码...

    @SuppressLint("MissingPermission")
    @Override
    protected void initView(Bundle savedInstanceState) {

        boolean deviceRooted = DeviceUtils.isDeviceRooted();//是否root
        if (!deviceRooted) {
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates("network", 0, 0, this);
        }

        mToolbar.setVisibility(View.GONE);
        ImmersionBar.with(this).titleBar(toolbar).init();   //适配刘海屏,自动设置状态栏距离和沉浸式状态栏
        StatusBarUtil.setAndroidNativeLightStatusBar(this, true);    //设置状态栏字体颜色

        String id = getIntent().getStringExtra("id");
        String contractName = getIntent().getStringExtra("contractName");
        mContractName.setText(contractName);
        mTitle.setText(contractName.replace(".pdf", ""));

        int playAudio = getIntent().getIntExtra("playAudio", 0);
        if (playAudio == 1) {
            playingmusic(1);
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();//获取纬度
        double longitude = location.getLongitude();//获取经度
        String strLatitude = String.valueOf(latitude);
        String strLongitude = String.valueOf(longitude);
        if (strLatitude != null && strLatitude != "") {
            double longti = Double.parseDouble(strLongitude);
            double latitu = Double.parseDouble(strLatitude);
            getAddress(longti, latitu);
            locationManager.removeUpdates(this);
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    public String getAddress(double lnt, double lat) {
        Geocoder geocoder = new Geocoder(this);
        boolean falg = geocoder.isPresent();
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //根据经纬度获取地理位置信息---这里会获取最近的几组地址信息,具体几组由最后一个参数决定
            List<Address> addresses = geocoder.getFromLocation(lat, lnt, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    //每一组地址里面还会有许多地址。这里我取的前2个地址。xxx街道-xxx位置
                    if (i == 0) {
                        stringBuilder.append(address.getAddressLine(i)).append("-");
                    }
                    if (i == 1) {
                        stringBuilder.append(address.getAddressLine(i));
                        break;
                    }
                }


                String country = address.getCountryName();// 国家
                String province = address.getAdminArea();//省
                String city = address.getLocality();//市
                String area = address.getSubLocality();//区
                String street = address.getFeatureName();//详细地址
                String id = getIntent().getStringExtra("id");
                LogUtils.d("1054  id="+id+"   经度"+lnt+"  纬度"+lat+"  国家"+country+"   省"+province+"  市"+
                    city+"   区"+area+"   详细"+street);
                //提交位置信息
                LocationBody locationBody=new LocationBody(id,String.valueOf(lnt),String.valueOf(lat),country,province,city,area,street);
                GlobalBody body = CustomEncryptUtil.customEncrypt(locationBody);
                RxManager.getMethod().location(body).compose(RxUtil.schedulers(FinishedContractActivity.this))
                        .subscribe(new RxCallback<Object>(FinishedContractActivity.this) {
                            @Override
                            public void onSuccess(Object o, String msg) {
                                LogUtils.d("1108  提交地理位置信息成功");
                            }
                        });
                return currentLocation;
            }
        } catch (IOException e) {
            LogUtils.d("定位失败");
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
阅读 4.5k
1 个回答

已解决,部分机型必须开启gps权限才能网络定位

推荐问题