Android片段中的位置权限

时间:2016-06-29 20:20:13

标签: java android android-fragments android-permissions

我希望这将是一个简单的解决方案,有人可以帮助我。

我有一个需要用户位置的应用,以便使用lat和lng来获取他们所在位置的天气。在我选择允许权限后,我能够获取用户位置,保留片段然后再次返回到该位置。问题在于我在弹出窗口中选择允许。我需要立即获得位置。希望我的代码是自我解释的。

我冒昧地删除了与我的问题无关的所有代码。此外,我试图调用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) {

}

2 个答案:

答案 0 :(得分:3)

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

片段中的requestPermissions方法将调用您在片段中定义的onRequestPermissionsResult,而您当前使用的方法将在您的活动中调用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);
}

答案 1 :(得分:0)

  

尝试此操作以获取片段

中的清单权限
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }