Search code examples
androidfusedlocationproviderclient

Android FusedLocationProviderClient doesn't always work


I tested this code with three phones. It works with two of them, but doesn't work with the third one, Huawei y9 2019. What's the problem? , And Why is it show (?) and (*) sign ??

My code is:

mFusedLocationProviderClient = new FusedLocationProviderClient(this);
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(500);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }

    mFusedLocationProviderClient.requestLocationUpdates(
            mLocationRequest,
            new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {


                    if(locationResult != null){
                        Log.i("Your_Location",locationResult.getLastLocation().toString()+"\n\nrequestLocationUpdates() isCall");
                        textView.setText(locationResult.getLastLocation().toString());
                    }
                    else{
                        Log.i("Your_Location"," requestLocationUpdates() isCall Please Give Permission and On Location");
                        textView.setText("Please Give Permission and On Location");
                    }
                    super.onLocationResult(locationResult);


                }
            },
            getMainLooper()


    );

enter image description here


Solution

  • I have checked with Huawei previous version and others phone it's working fine for me. But i have not Huawei y9 2019. So you may check with this code.

    //Request location through FusedLocationApi

    public void registerRequestUpdate(final LocationListener listener) {
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            localStorageInterval=sm.getLocalStorageInterval().get("localStorageInterval")*1000;
            mLocationRequest.setInterval(NOTIFY_INTERVAL/2); // every second
    
    
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, listener);
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                        if (!isGoogleApiClientConnected()) {
                            mGoogleApiClient.connect();
                        }
    
                        registerRequestUpdate(listener);
                     //   saveLocationAndSync();
    
                    }
                }
            }, NOTIFY_INTERVAL);
        }
    

    // get updated location from this method

     @Override
        public void onLocationChanged(Location location) {
            try {
                  string lat = location.getLatitude(); 
                  string long = location.getLongitude();
    
                }
            }catch (Exception ex){
                //exception
            }
        }