[英]Android PermissionsDispatch library returning null on location and not generating permisions
我厭倦了在Android中使用新的權限系統,因此開始尋找其他選項。 PermissionsDispatch庫似乎很棒,但我無法使它正常工作。 在過去的兩個小時中,我一直在嘗試使代碼無法正常工作。 這是一個相當小而簡單的示例:
@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();
}
}
我的問題是來自mLastLocation實例變量,即使在使用@NeedsPermission標記進行注釋之后,該變量仍返回null,並且根本不顯示其原理。 基本上,我想做的是通過同一教程來獲取Google https://developer.android.com/training/location/retrieve-current.html中的最后一個已知位置,但是使用此庫而不是編寫所有權限代碼。 我想念什么嗎? 我想要的是最后一個已知的位置。
嘗試使用此庫獲取用戶訪問位置權限
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.