2016-06-24 26 views
2

我正在嘗試創建一個應用程序。應用程序詢問每個時間位置在Marsh Mallow訪問,始終當我們打開應用程序。請提出解決我的問題的最佳流程。使用以下代碼 -刪除當前位置在棉花糖的運行時間權限

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
//for marshmallow 
    final int LOCATION_PERMISSION_REQUEST_CODE = 100; 
    ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, 
      LOCATION_PERMISSION_REQUEST_CODE); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    } 
    mMap.setMyLocationEnabled(true); 
    buildGoogleApiClient(); 
    mGoogleApiClient.connect(); 

} 

謝謝。

+0

您是否已經獲得許可?否則,在做這樣的工作之前徵求許可是正常的。 –

+0

其bczz你每次 –

+0

徵求許可要求的權限檢查其是否已經由用戶授予或不然後問..嘗試此之前的lib: https://github.com/UttamPanchasara/RuntimePermission –

回答

0

請自行反映您的代碼。考慮請求和檢查權限的順序。在你要求之前,你應該檢查許可。這可以確保防止詢問是否許可已得到批准...

看一看在example provided by Google並考慮將這個例子您的需求:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)  { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.READ_CONTACTS}, 
      MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

另外,你應該覆蓋回調方法onRequestPermissionsResult到處理用戶的grantResults

// Callback with the request from calling requestPermissions(...) 
@Override 
public void onRequestPermissionsResult(int requestCode, 
             @NonNull String permissions[], 
             @NonNull int[] grantResults) { 
    // Make sure it's our original READ_CONTACTS request 
    if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) { 
     if (grantResults.length == 1 && 
       grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show(); 
     } else { 
      Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show(); 
     } 
    } else { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 

編輯:

這是導出代碼,你應該改變

ActivityCompat.requestPermissions(this, new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION,  android.Manifest.permission.ACCESS_COARSE_LOCATION}, 
     LOCATION_PERMISSION_REQUEST_CODE); 

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

    return; 
} 

進一步的閱讀看看文章Understanding App Permissions

+0

if else條件我應該把 –

+0

看到編輯答案 –

0

嘗試書面方式中的if else,如果權限沒有給予要求或做你想做的事情。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      if(checkSelfPermission(Manifest.permission.Manifest.permission.ACCESS_FINE_LOCATION) 
         != PackageManager.PERMISSION_GRANTED) { 
        requestPermissions(new String[]{Manifest.permission.Manifest.permission.ACCESS_FINE_LOCATION}, 
          LOCATION_PERMISSION_REQUEST_CODE); 
       } 
      } else { 
       // do your stuff 
      } 

並且還覆蓋onRequestPermissionsResult並在您授予權限後應用您的代碼。

0

下面的代碼檢查,如果該應用程序有權限與地點工作,並請求所需的權限,如果:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
     Manifest.permission.ACCESS_FINE_LOCATION) 
!= PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
    Manifest.permission.ACCESS_FINE_LOCATION)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
     new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
     MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION_CODE); 

     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 
else 
{ 
    // Permission already granted ... This is where you can continue your further business logic... 
} 

而下面是回調方法:

@Override 
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION_CODE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
     && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       // permission was granted, yay! Do the 
       // Locatio-related task you need to do. 

      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

詳細解釋請訪問here(Android官方網頁)或this(Tutsplus教程)