gpt4 book ai didi

java - Android Fragment中的位置权限

转载 作者:行者123 更新时间:2023-12-01 21:26:25 27 4
gpt4 key购买 nike

我希望这将是一个简单的修复,并且有人可以帮助我。

我有一个应用程序需要用户位置,以便使用纬度和经度来获取他们所在位置的天气。在选择允许权限后,我能够获取用户位置,保留 fragment ,然后再次返回。问题在于当我在弹出窗口中选择允许时。我需要立即获取位置。希望我的代码是不言自明的。

我冒昧地删除了与我的问题无关的所有代码。此外,我尝试调用 onStart() 但没有成功。

public class FragmentLocalWeather extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private static final int PERMISSION_ACCESS_COARSE_LOCATION = 0;
private GoogleApiClient googleApiClient;

public FragmentLocalWeather() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.local_weather_fragment, container, false);

googleApiClient = new GoogleApiClient.Builder(this.getActivity(), this, this).addApi(LocationServices.API).build();

if (ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_ACCESS_COARSE_LOCATION);
}

setHasOptionsMenu(true);

return view;
}

@Override
public void onStart() {
super.onStart();
if (googleApiClient != null) {
googleApiClient.connect();
}
}

@Override
public void onStop() {
googleApiClient.disconnect();
super.onStop();

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_ACCESS_COARSE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// ********** problem Area **********
// Need to user location here.
} else {
Toast.makeText(this.getActivity(), "Need your location!", Toast.LENGTH_SHORT).show();
}
break;
}
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_local_weather, menu);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

switch (id) {
case R.id.action_settings: {

}
case R.id.action_refresh: {
onStart();
}
}

return true;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
if (ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

double lat = lastLocation.getLatitude(), lon = lastLocation.getLongitude();
Log.d("dozer74", "===========================>>>>>>>>>>>>> " + lat + " : " + lon);
}
}

@Override
public void onConnectionSuspended(int i) {

}

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

}

最佳答案

除了 Marcin 的建议(在问题区域中调用 googleApiClient.connect())之外,我认为您需要在 fragment 中使用 requestPermissions 方法而不是 ActivityCompat.requestPermissions。

fragment 中的 requestPermissions 方法将调用您在 fragment 中定义的 onRequestPermissionsResult,而您当前使用的方法将调用 Activity 内部的 onRequestPermissionsResult() (我猜您尚未重写)。

因此你的 onCreate 看起来更像是:

   if (ContextCompat.checkSelfPermission(this.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_ACCESS_COARSE_LOCATION);
}

关于java - Android Fragment中的位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38109331/

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