location.getSpeed() not coming for Marshmallow, using Fused Location API

喜你入骨 提交于 2019-12-21 23:52:21

问题


I'm using Fused Location API in my app ( getLatitude(), getLongitude(), getSpeed(), getElevation(), and distanceTo() ). It was working fine until I updated my phone (Moto X2 2014) to Marshmallow.

Now I don't receive speed in the location object in my app, rest methods are responding normally. However, if I run navigation on google maps in the background, I seem to receive speed inside my app as well.

Also, my app seems to work without any issues on Nexus 5X (Marshmallow) and other below API 23 phones.

What could be the problem? Has anyone faced something similar before?


回答1:


On devices running Android 6.0 (API level 23) and higher, you need to validate the permission programmatically. Youd could be something like this:

public class YourActivity extends AppCompatActivity implements
    LocationListener, ActivityCompat.OnRequestPermissionsResultCallback{

private static final int REQUEST_LOCATION = 0;
private static final String[] PERMISSION_LOCATION = {Manifest.permission.ACCESS_FINE_LOCATION};
private LocationManager locManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);

    //Check if the Location permission is already available.
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
        //Permission not granted.
        requestLocationPermission();

    } else {
        //Permission granted.
        initLocManager();
    }

    }

}


/**
 * Requests the permission.
 */
private void requestLocationPermission() {

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.ACCESS_FINE_LOCATION)) {
        //Permission has been previously denied.
        //Show message...
    } else {
        //Permission has not been granted yet. It is requested directly.
        ActivityCompat.requestPermissions(this, PERMISSION_LOCATION, REQUEST_LOCATION);
    }
}


/**
 * Callback.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {

    if (requestCode == REQUEST_LOCATION) {

        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Permission granted.
            initLocManager();
        } else {
            //Permission not granted.
        }

    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}


public void initLocManager(){

    //Sample code...

    map.setMyLocationEnabled(true);

    locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    if(locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) ||
            locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission("android.permission.ACCESS_FINE_LOCATION") == PackageManager.PERMISSION_GRANTED) {
                locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            }
        }else{
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
    } else{
        Toast.makeText(this, "No location services", Toast.LENGTH_LONG).show();
    }
}


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

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onProviderDisabled(String provider) {}

@Override
public void onLocationChanged(Location location) {
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),
            location.getLongitude()), 16));
    removeUpdatesLocListener();
}


private void removeUpdatesLocListener(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission("android.permission.ACCESS_FINE_LOCATION") == PackageManager.PERMISSION_GRANTED) {
            if(locManager!=null)
                locManager.removeUpdates(this);
        }
    }else{
        if(locManager!=null)
            locManager.removeUpdates(this);
    }
}


@Override
protected void onStop() {
    super.onStop();
    removeUpdatesLocListener();
}

}

Good luck!

And if you need more information, look this



来源:https://stackoverflow.com/questions/34589659/location-getspeed-not-coming-for-marshmallow-using-fused-location-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!