我正在开发一个Android应用程序,因为我正在获取用户的当前位置,以下代码在7.1.1版本的android下工作正常但不能在7.1.1上工作,我的位置为null
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
List<String> providers = locationManager.getAllProviders();
Location location = null;
if (android.os.Build.VERSION.SDK_INT >= 25)
{
location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
}
else {
for (String str : providers) {
location = locationManager.getLastKnownLocation(str);
if (location != null)
break;
}
}
如何在android 7.1.1版本中获取当前位置?
答案 0 :(得分:2)
为什么不像@thepoosh那样使用FusedLocationProvider
。
以下是有关FusedLocationProvider的所有信息的参考资料。
清单中的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
运行时权限(这是我使用的代码 - &gt;不是最干净的,但它应该可以帮助您启动...)
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = "MainActivity";
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
private LocationRequest mLocationRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
//Here is your location result
}
};
mLocationRequest = LocationHelper.createLocationRequest();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
if (LocationHelper.checkLocationPermissions(this)) {
startGps();
} else {
requestPermissions();
}
}
private void requestPermissions() {
boolean shouldProvideRationale =
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION);
// Provide an additional rationale to the user. This would happen if the user denied the
// request previously, but didn't check the "Don't ask again" checkbox.
if (shouldProvideRationale) {
Log.i(TAG, "Displaying permission rationale to provide additional context.");
View container = findViewById(android.R.id.content);
DialogHelper.showSnackbar(this, R.string.permission_rationale, android.R.string.ok,
new View.OnClickListener() {
@Override
public void onClick(View view) {
// Request permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
});
} else {
Log.i(TAG, "Requesting permission");
// Request permission. It's possible this can be auto answered if device policy
// sets the permission in a given state or the user denied the permission
// previously and checked "Never ask again".
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_PERMISSIONS_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.i(TAG, "onRequestPermissionResult");
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.i(TAG, "User interaction was cancelled.");
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Permission granted.");
startGps();
} else {
// Permission denied.
// Notify the user via a SnackBar that they have rejected a core permission for the
// app, which makes the Activity useless. In a real app, core permissions would
// typically be best requested during a welcome-screen flow.
// Additionally, it is important to remember that a permission might have been
// rejected without asking the user for permission (device policy or "Never ask
// again" prompts). Therefore, a user interface affordance is typically implemented
// when permissions are denied. Otherwise, your app could appear unresponsive to
// touches or interactions which have required permissions.
//TODO: SHOW SNACKBAR
}
}
}
private void startGps()
{
Log.i(TAG, "Setting up location services...");
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null).addOnSuccessListener(this).addOnFailureListener(this);
}
}
LocationHelper:
public class LocationHelper {
private static final String TAG = "LocationHelper";
private static final int UPDATE_INTERVAL = 4000;
private static final int FASTEST_UPDATE_INTERVAL = 2000;
public static LocationRequest createLocationRequest()
{
LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(UPDATE_INTERVAL);
locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return locationRequest;
}
public static boolean checkLocationPermissions(Context context) {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
}