2015-10-18 59 views
0

我寫了簡單的代碼,返回設備的gpsLocation。 當我編譯它時,我得到一個關於我沒有使用訪問GPS和網絡的'checkSelfPermission'的錯誤。爲什麼我不能在代碼中刪除'checkSelfPermission'?

當我到達'checkSelfPermission'的代碼時,我得到一個異常。

  1. 我該如何避免'必須'使用checkSelfPermission方法?
  2. 爲什麼我得到異常?

    @TargetApi(Build.VERSION_CODES.M) 
        private void getLocation() { 
        try { 
          locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE); 
    
        // getting GPS status 
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    
        // getting network status 
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
    
        if (isNetworkEnabled) 
        { 
         **// the exception is on the 'checkSelfPermission'** 
          if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
    
           return; 
          } 
    
         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
           MIN_TIME_BW_UPDATES, 
           MIN_DISTANCE_CHANGE_FOR_UPDATES, 
           this); 
    
         if (locationManager != null) 
         { 
    
          location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
          if (location != null) { 
           latitude = location.getLatitude(); 
           longitude = location.getLongitude(); 
          } 
         } 
        } 
    
        if (isGPSEnabled) 
        { 
         if (location == null) 
         { 
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
            MIN_TIME_BW_UPDATES, 
            MIN_DISTANCE_CHANGE_FOR_UPDATES, 
            this); 
    
          if (locationManager != null) 
          { 
           location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    
           if (location != null) 
           { 
            latitude = location.getLatitude(); 
            longitude = location.getLongitude(); 
           } 
          } 
         } 
        } 
    } 
    catch(Exception e) 
    { 
        Log.e("Error", e.getMessage()); 
    } 
    

    }

回答

2
從Android文檔

直接:

期初的Android 6.0(API等級23),用戶權限授予 的應用程序,而應用程序正在運行,不當他們安裝應用程序。

系統權限分爲兩類,普通和 危險:

1.Normal權限不直接風險用戶的隱私。如果您的 應用在其清單中列出了正常權限,系統將自動授予 權限。

2.危險權限可以讓應用程序 訪問用戶的機密數據。

如果您的應用程序需要危險權限,則每次執行需要該權限的操作時都必須檢查您是否擁有該權限。訪問設備位置需要一個危險的權限,因此您必須在運行時檢查並請求位置權限。

要檢查你的應用程序已經被授予的權限,你可以使用:

// Assume thisActivity is the current activity 
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, 
     Manifest.permission.WRITE_CALENDAR); 

如果應用許可,該方法返回PackageManager.PERMISSION_GRANTED,以及應用程序可以繼續操作。如果該應用程序沒有權限,該方法返回PERMISSION_DENIED,並且該應用程序必須明確要求用戶許可。因此你不能避免checkSelfPermission

+0

我不能在eclipse中使用ContextCompat.checkSelfPermission你告訴我如何使用你的解決方案在eclipse中使用運行時權限。 – Vasant

+0

使用eclipse並沒有改變,運行期間詢問權限的方式保持不變。 –

+0

在eclipse中,runtim permition的代碼將無法正常工作。我正在使用v4庫。您可以爲我提供最新的支持庫 – Vasant

相關問題