【问题标题】:location.getSpeed() not coming for Marshmallow, using Fused Location API使用 Fused Location API,Marshmallow 不会使用 location.getSpeed()
【发布时间】:2016-04-07 23:27:49
【问题描述】:

我在我的应用中使用 Fused Location API(getLatitude()、getLongitude()、getSpeed()、 getElevation() 和 distanceTo() )。在我将手机 (Moto X2 2014) 更新为 Marshmallow 之前,它运行良好。

现在我在我的应用程序的位置对象中没有收到速度,其余方法正在正常响应。但是,如果我在后台在谷歌地图上运行导航,我似乎也在我的应用程序中获得了速度。

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

可能是什么问题?有没有人遇到过类似的情况?

【问题讨论】:

  • 我也遇到了同样的问题,你找到解决办法了吗?

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


【解决方案1】:

在运行 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

【讨论】:

  • 我设置的 targetSDKVersion 是 22。我已经提到它对于在 Android 6.0 上运行的其他设备运行良好(无需在应用内请求权限)
猜你喜欢
  • 2017-05-04
  • 2016-07-09
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 2015-08-17
  • 2015-12-02
  • 2017-05-30
  • 2017-05-02
相关资源
最近更新 更多