gpt4 book ai didi

android - 询问位置许可后,应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:03:34 26 4
gpt4 key购买 nike

我知道之前曾问过同样的问题,但在我的情况下我找不到任何好的答案。基本上,当我单击具有当前位置应用程序的 Activity 时,该应用程序正在停止,然后征求许可。当我允许它时,应用程序将继续正常运行。另一件事是我没有收到错误。

 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

mService = Common.getGoogleAPIService();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
checkLocationPermission();
}
}

private boolean checkLocationPermission()
{
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION))
ActivityCompat.requestPermissions(this,new String[]{

Manifest.permission.ACCESS_FINE_LOCATION

},MY_PERMISSION_CODE);
else
ActivityCompat.requestPermissions(this,new String[]{

Manifest.permission.ACCESS_FINE_LOCATION

},MY_PERMISSION_CODE);
return false;
}
else
return true;
}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
buildGoogleApiClien();
mMap.setMyLocationEnabled(true);
}
else
{
buildGoogleApiClien();
mMap.setMyLocationEnabled(true);
}
}


}

private synchronized void buildGoogleApiClien()
{
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}
}

@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}

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

}

@Override
public void onLocationChanged(Location location) {


mLastLocation = location;
if (mMarker != null)

mMarker.remove();

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

LatLng latLng = new LatLng(latitude,longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng);
// .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
// mMarker = mMap.addMarker(markerOptions);

最佳答案

在AndroidManifest.xml文件中添加位置权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

关于android - 询问位置许可后,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53154092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com