OnRequestPermissionsResultCallback不在PreferenceFragment中触发

时间:2016-06-21 03:02:05

标签: java android permissions android-permissions

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它从未被调用,无论是拒绝还是接受。

有什么想法吗?这些示例都使用Activity而不是Fragment,因此可能是问题所在。另外,我不确定我应该使用哪个Compat(v4 vs v13)。 Min SDK是14。

1 个答案:

答案 0 :(得分:2)

完整代码:

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);
        }


    }
}