2017-06-12 46 views
2

當我的片段被充氣時,它會根據android 6.0+中的要求打開dialog box獲取權限WRITE_EXTERNAL_STORAGE。我已經關注了每個教程,但是當授予或拒絕權限時,我的任何操作都不會執行。 我想要關閉應用程序,並在拒絕權限時顯示敬酒,即應用程序重新打開時的permission would be asked again行動不起作用權限被拒絕

片段開始:

public class SecondFragment extends Fragment { 

View myView; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    myView = inflater.inflate(R.layout.second_layout, container, false); 
    configureImageButton(); 
    ((MainActivity) getActivity()).setActionBarTitle("App"); 

    if (ContextCompat.checkSelfPermission(getActivity(), 
      Manifest.permission.WRITE_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 
     if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), 
       Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
     } else { 
      ActivityCompat.requestPermissions(getActivity(), 
        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        1); 
     } 
    } 

onRequestPermissionsResult:

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { 
    switch (requestCode) { 
     case 1: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       Snackbar.make(myView, "Permission Granted.", Snackbar.LENGTH_LONG).show(); 
      } else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) { 
       Toast.makeText(getActivity(), "Reopen app and allow permission.", Toast.LENGTH_LONG).show(); 
       getActivity().finish(); 
      } 
    } 
} 

該應用程序不准許/拒絕許可但忽略了烤麪包或小吃吧。 所有的建議表示讚賞。謝謝!

+0

問題出在'ActivityCompat.requestPermissions'上。在你的情況下'onRequestPermissionsResult'將在Activity中被調用,而不是在Fragment中。當你使用片段的'requestPermissions'函數時,它會調用片段的'onRequestPermissionsResult'。 – aborocz

+0

@aborocz你的解釋很有意義,謝謝,我將代碼移到了我的活動中,而不是從片段內部工作。現在它完美地工作。謝謝。 –

回答

0

內片段的一些變化是有 PLZ嘗試像下面

private void checkLocationPermission() { 
    if (ContextCompat.checkSelfPermission(this.mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 


      //Prompt the user once explanation has been shown 
      requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        MY_PERMISSIONS_REQUEST_CODE); 


     } else { 
      // No explanation needed, we can request the permission. 
      requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        MY_PERMISSIONS_REQUEST_CODE); 
     } 
    } 
} 

其中MY_PERMISSIONS_REQUEST_CODE = 1你的情況

+0

我嘗試插入上面的代碼,但它需要API級別23+,並且在我正在使用(並希望繼續使用)API級別19+的應用程序中,是否有解決此問題的方法?謝謝 –

+0

如果API 19然後無需檢查任何permition becase應用程序permition從23 api開始 –

+0

哈哈當然,我只是沒有想到。謝謝! –

0

(1)如果您的應用程序已得到批准,可以不受此看到的權限對話框碼。

if (ContextCompat.checkSelfPermission(getActivity(), 
     Manifest.permission.WRITE_EXTERNAL_STORAGE) 
     != PackageManager.PERMISSION_GRANTED) 

所以,進入設置 - 應用程序 - 您的應用 - 和禁用已授予的權限,然後再次運行你的應用程序。

(2)您的onCreateView是否正確返回myView?在你的示例代碼中,沒有「返回myView;」

0
public static final int RequestPermissionCodeStorage = 105; // Declare Global Variable  

    public boolean checkStoragePermission() { 

       if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

        int result = ContextCompat.checkSelfPermission(this, 
          Manifest.permission.WRITE_EXTERNAL_STORAGE); 
        if (result == PackageManager.PERMISSION_GRANTED) { 
         return true; 
        } else { 
         ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, RequestPermissionCodeStorage); 
         return false; 
        } 
       } else { 
        return true; 
       } 
      } 


    @Override 
     public void onRequestPermissionsResult(int requestCode, 
               String permissions[], int[] grantResults) { 
      switch (requestCode) { 

       case RequestPermissionCodeStorage: { 

        if (grantResults.length > 0 
          && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        } else { 
         Toast.makeText(this, "Allow Permission of Storage", Toast.LENGTH_LONG).show(); 
         finish(); 
        } 
        return; 
       } 
      } 
     }