location.getSpeed()不适用于Marshmallow,使用Fused Location API [英] location.getSpeed() not coming for Marshmallow, using Fused Location API

查看:188
本文介绍了location.getSpeed()不适用于Marshmallow,使用Fused Location API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用Fused Location API( getLatitude() getLongitude() getSpeed() getElevation()和 distanceTo())。直到我将我的手机(Moto X2 2014)更新为Marshmallow。



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



另外,我的应用程序似乎工作没有任何问题Nexus 5X(棉花糖)和其他API 23以下的手机。



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

解决方案

在运行 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};
私人LocationManager locManager;


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

//检查位置权限是否已经可用。
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
//未授予权限。
requestLocationPermission();

}其他{
//授予权限。
initLocManager();





$ b $ **
*请求许可。
* /
private void requestLocationPermission(){

if(ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)){
//权限之前已被拒绝。
//显示消息...
} else {
//权限尚未被授予。它是直接要求的。
ActivityCompat.requestPermissions(this,PERMISSION_LOCATION,REQUEST_LOCATION);
}
}


/ **
*回拨。
* /
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String []权限,
@NonNull int [] grantResults){

if(requestCode == REQUEST_LOCATION){

if(grantResults.length == 1&& amp; grantResults [0] == PackageManager.PERMISSION_GRANTED){
//授予的权限。
initLocManager();
} else {
//未授予权限。
}

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



$ b public void initLocManager(){

//示例代码...

map.setMyLocationEnabled(true);

locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
$ b $ 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();



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

@Override
public void onProviderEnabled(String provider){}
$ b @Override
public void onProviderDisabled(String provider){}

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


$ b 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);



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

}

祝你好运!



如果您需要更多信息,看看这个


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?

解决方案

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

这篇关于location.getSpeed()不适用于Marshmallow,使用Fused Location API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆