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

回眸只為那壹抹淺笑 提交于 2019-12-04 19:16:47

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

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