2016-06-21 87 views
0
import android.preference.PreferenceFragment; 
import android.support.v13.app.FragmentCompat; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 

public class Preferences extends PreferenceFragment implements OnPreferenceClickListener, FragmentCompat.OnRequestPermissionsResultCallback { 
if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
    != PackageManager.PERMISSION_GRANTED) { 
ActivityCompat.requestPermissions(activity, 
     new String[]{ android.Manifest.permission.WRITE_EXTERNAL_STORAGE }, 
     Constant.WRITE_STORAGE_PERMISSION); 

} 

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grants) { } 

我試圖獲得新的權限。當它應該時,對話框會顯示請求權限,但是onRequestPermissionResult從來不會被調用,無論是拒絕還是接受。OnRequestPermissionsResultCallback在PreferenceFragment中未觸發

任何想法?這些例子都是使用Activity而不是Fragment,所以這可能是問題所在。另外,我不確定我應該使用哪個Compat(v4 vs v13)。閔SDK是14

回答

1

爲您全碼:

public interface PermissionResultListener { 
     public void onPermissionResult(int requestCode, 
             String permissions[], int[] grantResults); 
    } 



    public class Preferences extends PreferenceFragment implements Preference.OnPreferenceClickListener, 
     PermissionResultListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    public void requestPermission() { 

     Activity activity = getActivity(); 
     if (activity != null && activity instanceof MainActivity) { 
      MainActivity mainActivity = (MainActivity) activity; 
      mainActivity.requestPermissionStorage(); 
      mainActivity.setPermissionResultListener(this); 
     } 

    } 

    @Override 
    public boolean onPreferenceClick(Preference preference) { 
     return false; 
    } 

    @Override 
    public void onPermissionResult(int requestCode, String[] permissions, int[] grantResults) { 
     // request permission result here 
    } 
} 

public class MainActivity extends AppCompatActivity { 

    private PermissionResultListener mPermissionResultListener; 

    public void setPermissionResultListener(PermissionResultListener mPermissionResultListener) { 
     this.mPermissionResultListener = mPermissionResultListener; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Check that the activity is using the layout version with 
     // the fragment_container FrameLayout 
     if (findViewById(R.id.fragment_container) != null) { 

      // However, if we're being restored from a previous state, 
      // then we don't need to do anything and should return or else 
      // we could end up with overlapping fragments. 
      if (savedInstanceState != null) { 
       return; 
      } 

      // Create a new Fragment to be placed in the activity layout 
      Preferences preferences = new Preferences(); 

      // In case this activity was started with special instructions from an 
      // Intent, pass the Intent's extras to the fragment as arguments 
      preferences.setArguments(getIntent().getExtras()); 

      // Add the fragment to the 'fragment_container' FrameLayout 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.fragment_container, preferences).commit(); 
     } 

    } 

    public void requestPermissionStorage() { 

     if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        Constant.WRITE_STORAGE_PERMISSION); 
     } 

    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

     if (mPermissionResultListener != null) { 
      mPermissionResultListener.onPermissionResult(requestCode, permissions, grantResults); 
     } 


    } 
} 
+0

相同的結果。有什麼不同?問題是'public void onRequestPermissionsResult(int requestCode,@NonNull String [] permissions,@NonNull int [] grantResults){}'永遠不會被調用。 – xBlue

+0

另外,什麼是'activity.setPermissionResultListener()'和'permissionResultListener.onPermissionResult'? – xBlue

+0

@xBlue,public void onRequestPermissionsResult(int requestCode,String [] permissions,int [] grantResults){},此方法僅在活動中調用,而不是在片段中調用,因此您必須在活動中實現此方法,片段,您必須創建接口PermissionResultListener。意思是在你的活動中,你必須聲明變量permissionResultListener和方法public void setPermissionResultListener(PermissionResultListener permissionResultListener) –