2017-04-18 49 views
0

我厭倦了在Android中使用新的權限系統,因此開始尋找其他選項。 permissionsDispatch庫看起來很神奇,但我無法讓它工作。我一直試圖在過去的兩個小時內讓代碼無效。這是一個相當小的和簡單的例子:Android PermissionsDispatch庫在位置上返回null並且不會產生權限

@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。基本上,我想要做的是通過相同的教程獲取谷歌https://developer.android.com/training/location/retrieve-current.html內的最後一個已知位置,但使用此庫而不必編寫所有的權限代碼。我錯過了什麼嗎?我想要的只是最後一個已知的位置。

+0

你加這個'<使用許可權的android:NAME =「android.permission.ACCESS_FINE_LOCATION」 />清單中'? – rafsanahmad007

+0

@ rafsanahmad007的確,我還啓用了註釋處理器和一切。最後剩下的部分是查看代碼出了什麼問題。我在圖書館的主github回購庫中提交了一個問題(它被改爲一個問題,並且沒有結果),但它仍然不起作用 –

+0

是否有任何異常?顯示權限對話框? – rafsanahmad007

回答

0

嘗試使用this庫獲取用戶接入位置的權限

+0

聽起來像個好主意。我仍然想讓這個工作,並知道爲什麼它不工作。 –

相關問題