2016-10-26 68 views
1

有誰知道缺少的權限是如何運作的以及它何時顯示在logcat中?權限被拒絕(缺少INTERNET權限?)未出現在logcat中

我試圖有意刪除INTERNET權限以觸發此異常,但在下面的httpsURLConnection.connect()期間並沒有被觸發 - 發生的是它直接進入finally塊。

最初我以爲這是因爲之前授予權限,應用程序/測試設備會記住它,所以我卸載了應用程序然後重新安裝了它,但同樣的事情發生。

有誰知道是什麼觸發了這種行爲?謝謝!

編輯:我有另一個應用程序(來自Udacity課程的Sunshine應用程序),我複製了這段代碼,並且顯示了權限錯誤。從我的類

摘譯 - 在httpsURLConnection.connect()行

public class MovieDBAPI extends AsyncTask<String, Object, List<Movie>> { 

    final String TAG = getClass().getSimpleName(); 

    protected List<Movie> doInBackground(String... params) { 

     BufferedReader bufferedReader = null; 
     HttpsURLConnection httpsURLConnection = null; 
     StringBuffer stringBuffer = null; 
     try { 

      //create a URL 
      URL url = new URL(buildURL(params[0])); 
      Log.v(TAG, url.toString()); 
      httpsURLConnection = (HttpsURLConnection) url.openConnection(); 
      httpsURLConnection.setRequestMethod("GET"); 
      httpsURLConnection.connect(); 

      //get string input 
      InputStream inputStream = httpsURLConnection.getInputStream(); 
      if (inputStream == null) { 
       //no input stream, nothing to do 
       return null; 
      } 
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      String line; 
      stringBuffer = new StringBuffer(); 
      while((line = bufferedReader.readLine()) != null) { 
       // Since it's JSON, adding a newline isn't necessary (it won't affect parsing) 
       // But it does make debugging a *lot* easier if you print out the completed 
       // buffer for debugging. 
       stringBuffer.append(line + "\n"); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 

     } finally { 

      //if stringBuffer is not null, then prepare the result 
      if (stringBuffer != null) { 
       return getMovieDataFromJSON(stringBuffer.toString()); 
      } 

      if (httpsURLConnection != null) { 
       httpsURLConnection.disconnect(); 
      } 

      if (bufferedReader != null) { 
       try { 
        bufferedReader.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      return null; 

     } 

    } 

} 
+3

以任何機會,你在棉花糖運行此代碼以上? –

+0

@TheAbsurd其實牛軋糖。 7.0 –

回答

0

對於Android的M和上述期待權限被拒絕(缺少INTERNET權限?),查看Android文檔..

你並不需要許可准入正常權限 https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

https://developer.android.com/guide/topics/security/normal-permissions.html

+0

感謝分享。我已經更新了我的問題以提供更多的上下文 - 我有一個具有相同代碼的前一個應用程序,但那個觸發了權限異常,這就是爲什麼我對這種當前行爲感到困惑。 –

+0

您是否在同一設備上嘗試使用Sunshine應用程序?由於重要的一點是設備版本(Android 6及以上版本)。 –

+0

是的,這是我之前測試的應用程序,當我刪除互聯網許可時,出現異常。 –

0

對於棉花糖及以上版本,您需要在運行時請求權限。在AndroidManifest.xml中提到他們是沒有必要的,

請參考以下鏈接, https://developer.android.com/training/permissions/requesting.html

+0

感謝分享。我已經更新了我的問題以提供更多的上下文 - 我有一個具有相同代碼的前一個應用程序,但那個觸發了權限異常,這就是爲什麼我對這種當前行爲感到困惑。 –

-1

至於棉花糖及以上的Android有一個新的運行時間許可制度。你在運行時要求。將以下代碼片段添加到您的課程中,這將幫助您詢問運行時權限。 private int INTERNET_PERMISSION_CODE = 23;

//We are calling this method to check the permission status 
    private boolean isInternetnAllowed() { 
     //Getting the permission status 
     int result = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET); 

     //If permission is granted returning true 
     if (result == PackageManager.PERMISSION_GRANTED) 
      return true; 

     //If permission is not granted returning false 
     return false; 
    } 

    //Requesting permission 
    private void requestInternetPermission(){ 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INTERNET)){ 
      //If the user has denied the permission previously your code will come to this block 
      //Here you can explain why you need this permission 
      //Explain here why you need this permission 
     } 

     //And finally ask for the permission 
     ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, INTERNET_PERMISSION_CODE); 
    } 

    //This method will be called when the user will tap on allow or deny 
    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 

     //Checking the request code of our request 
     if(requestCode == INTERNET_PERMISSION_CODE){ 

      //If permission is granted 
      if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 

       //Displaying a toast 
       Toast.makeText(this,"Permission granted for internet",Toast.LENGTH_LONG).show(); 
      }else{ 
       //Displaying another toast if permission is not granted 
       Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

然後什麼都你正在做doInBackground方法

做到像這樣

if(isInternetnAllowed()){ 
    //do your doInbackground stuff 
    }else { 
     requestInternetPermission(); 
    } 

希望它可以幫助

+0

感謝分享。我已經更新了我的問題以提供更多的上下文 - 我有一個具有相同代碼的前一個應用程序,但那個觸發了權限異常,這就是爲什麼我對這種當前行爲感到困惑。 –

+0

您是否提供顯示權限? –

+0

我刪除它,因爲我想看到logcat中出現丟失的權限異常。但是即使沒有它,logcat中也不會出現異常。 –

相關問題