如何在Android Api级别23中实现融合位置提供程序

时间:2017-05-24 17:36:46

标签: android

我正在尝试使用Fused Location提供程序在我的应用程序中获得连续的位置更新,但它在Lollipop中运行良好,但在Marshmallow中它没有显示任何位置更新。

下面是我的代码.Plz让我知道我错了什么。

public class Gps extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

TextView lati, longi;
LocationRequest lr;
GoogleApiClient mGoogleApiClient;
double latitude = 0, longitude = 0;

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

    lati = (TextView) findViewById(R.id.lati);
    longi = (TextView) findViewById(R.id.longi);

    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();


}

@Override
protected void onStart() {
    super.onStart();

    mGoogleApiClient.connect();
}

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

    if (mGoogleApiClient.isConnected()) {

        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    lr = LocationRequest.create();
    lr.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    lr.setInterval(1000);

    if (Build.VERSION.SDK_INT >= 23) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            requestPermissions(new String[]{

                    Manifest.permission.ACCESS_FINE_LOCATION
            }, 1);

            return;
        }

    }
    else{

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, lr,this);
    }
}

@Override
public void onLocationChanged(Location location) {

    latitude = location.getLatitude();
    longitude = location.getLongitude();

    Geocoder gc = new Geocoder(Gps.this, Locale.getDefault());

    String locality = null;
    String sub = null;

    try {

        List<Address> addrList;

        addrList = gc.getFromLocation(latitude, longitude, 2);

        if (addrList.size() > 0) {

            Address adr = addrList.get(0);
            locality = adr.getLocality();
            sub = adr.getSubLocality();

            lati.setText(locality);
            longi.setText(sub);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {

        case 2:
            if(){
          }                
           else{

                  }
    }

}

}

1 个答案:

答案 0 :(得分:0)

您无法在onRequestPermissionsResult功能中请求位置更新。此外,即使您检查了两者,也只是请求ACCESS_FINE_LOCATION而不是ACCESS_COARSE_LOCATION

修改

onConnected功能中,当您在Marshmallow或以上时,您永远不会致电LocationServices.FusedLocationApi.requestLocationUpdates()。基本上你只是这样做:

if (Build.VERSION.SDK_INT >= 23) {
    if (/*No permissions*/) {
        //Request permissions
        return;
    }
    //Location updates are never requested here!!!
}
else {
    //Request location updates
}

什么时候应该是这样的:

if (Build.VERSION.SDK_INT >= 23) {
    if (/*No permissions*/) {
        //Request permissions
        return;
    }
}
//Request location updates