使用Fused Location API,location.getSpeed()不会用于Marshmallow

时间:2016-01-04 11:03:11

标签: android gps android-6.0-marshmallow android-location

我在我的应用中使用了Fused Location API( getLatitude() getLongitude() getSpeed(),< em> getElevation()和 distanceTo())。在我将手机(Moto X2 2014)更新为Marshmallow之前,它工作正常。

现在我没有在我的应用中的位置对象中获得速度,其他方法正常响应。但是,如果我在后台运行Google地图导航,我似乎也会在我的应用中获得速度。

此外,我的应用似乎在Nexus 5X(Marshmallow)和其他API 23手机上没有任何问题。

可能是什么问题?有没有人面对过类似的事情?

1 个答案:

答案 0 :(得分:0)

在运行 Android 6.0 (API级别23)及更高级别的设备上,您需要验证 权限 以编程方式。 你可能是这样的:

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();
}

}
祝你好运!

如果您需要更多信息,请look this