使用 FusedLocationProviderClient,不会在 Lollipop Emulator 上调用 locationCallback。模拟器不包含 GOOGLE Play Services。弹出启用位置服务的 POP UP 并调用 startLocationUpdates() 方法但未调用回调。FusedLocationAPI 是否应该在没有 Goodle Play 服务的情况下工作?
public class BasicHomeActivity extends AppCompatActivity {
private static final int REQUEST_CHECK_SETTINGS = 100;
private FrameLayout basicHomeFrameLayout;
private int LOCATION_REQUEST_CODE = 200;
private LinearLayout rootLayout;
private ActionBar actionBar;
private View actionView;
private ProgressBar locationLoader;
private FusedLocationProviderClient fusedLocationClient;
private LocationCallback locationCallback;
private static String TAG = BasicHomeActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_home);
BottomNavigationView navView = findViewById(R.id.nav_view);
basicHomeFrameLayout = findViewById(R.id.basic_home_fragment);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
rootLayout = (LinearLayout) findViewById(R.id.basic_home_root_layout);
//Adding Fragment to Home Activity
replaceFragment(R.id.basic_home_fragment,new HomeFragment());
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionView = inflator.inflate(R.layout.custom_actionbar_layout, null);
actionBar.setCustomView(actionView);
locationLoader = (ProgressBar) actionView.findViewById(R.id.location_loader);
locationLoader.getIndeterminateDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Log.e("Location Callback","true");
if (locationResult == null) {
Log.e("Found Location","False");
return;
}
for (Location location : locationResult.getLocations()) {
Log.e("Found Location","True");
findCompleteAddress(location);
fusedLocationClient.removeLocationUpdates(locationCallback);
}
};
};
checkFusedLocationSettings();
}
private void checkFusedLocationSettings() {
LocationRequest locationRequest = createLocationRequest();
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
Log.e("Location Settings","true");
startLocationUpdates();
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("Location Failure",e.getMessage());
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(BasicHomeActivity.this,
REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
}
public void startLocationUpdates() {
Log.e("Starting Location","true");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
LOCATION_REQUEST_CODE);
Log.e("Permission Granted","false");
}
else {
Log.e("Permission Granted","true");
locationLoader.setVisibility(View.VISIBLE);
fusedLocationClient.requestLocationUpdates(createLocationRequest(),
locationCallback,
null /* Looper */);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 200: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationLoader.setVisibility(View.VISIBLE);
fusedLocationClient.requestLocationUpdates(createLocationRequest(),
locationCallback,
null /* Looper */);
}
}
}
}
}
protected LocationRequest createLocationRequest() {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return locationRequest;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CHECK_SETTINGS){
if(resultCode == Activity.RESULT_OK){
startLocationUpdates();
}
}
}
}