I got tired of playing around with the new permission system in Android and as such started to look for other options. The permissionsDispatch library seems amazing but I can't get it to work. I have been trying for the past two hours to get the code to work to no avail. It is a fairly small and simple example:
@RuntimePermissions
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final int MY_REQUEST_PERMISSION_COARSE_LOCATION = 45;
public static final String TAG = "MAINACTIVITYITS";
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleApiBuilder();
}
public void GoogleApiBuilder() {
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@SuppressWarnings("MissingPermission")
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
public void mLocationSetter(){
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationSetter();
Log.i(TAG, mLastLocation.getLatitude() + "");
Toast.makeText(this, mLastLocation.getLatitude() + "" , Toast.LENGTH_LONG).show();
}
@OnShowRationale(Manifest.permission.ACCESS_COARSE_LOCATION)
public void locationRationale(PermissionRequest request){
showRationaleDialog(R.string.permission_coarse_rationale, request);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
private void showRationaleDialog(@StringRes int messageResId, final PermissionRequest request){
new AlertDialog.Builder(this)
.setPositiveButton(R.string.button_allow, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.proceed();
}
})
.setNegativeButton(R.string.button_deny, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
request.cancel();
}
})
.setCancelable(false)
.setMessage(messageResId)
.show();
}
}
My issue here comes from the mLastLocation instance variable returning null even after being annotated with the @NeedsPermission tag and the rationale is not showing at all. Basically, what I am trying to do is go by the same tutorial for getting the last known location inside Google https://developer.android.com/training/location/retrieve-current.html but using this library instead of having to write all the code for permissions. Am I missing something? All I want is the last known location.
尝试使用此库获取用户访问位置权限
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.