我正在更新我们的应用程序,以使用新的M运行时权限系统。 除了onRequestPermissionsResult()之外,它都在工作。我需要检查按下按钮的权限,如果成功,就发送一条短信。当我授权这样做时,对话框会关闭,但直到我再次按下按钮,它才会触发“发送文本”。

我已经在onRequestPermissionsResult()方法中调试和设置了断点,但它从未进入它。

首先调用这个方法:

    private void askForPermission() {
    String[] permissions = new String[]{Manifest.permission.SEND_SMS};
    ActivityCompat.requestPermissions(getActivity(), permissions, PERMISSIONS_CODE);
}

然后我的回调函数是这样的:

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

    if (requestCode == PERMISSIONS_CODE) {
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            int grantResult = grantResults[i];

            if (permission.equals(Manifest.permission.SEND_SMS)) {
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    onPPSButtonPress();
                } else {
                    requestPermissions(new String[]{Manifest.permission.SEND_SMS}, PERMISSIONS_CODE);
                }
            }
        }
    }
}

有人遇到过类似的问题吗? 感谢任何帮助。 谢谢


当前回答

 /* use the object of your fragment to call the 
 * onRequestPermissionsResult in fragment after
 * in activity and use different request Code for 
 * both Activity and Fragment */

   if (isFragment)
    mFragment.requestPermissions(permissions.toArray(new
    String[permissions.size()]),requestPermission);

   else
    ActivityCompat.requestPermissions(mActivity,permissions.toArray(new
    String[permissions.size()]),requestPermission);

其他回答

如果出于某种原因,你扩展了一个自定义Activity,这个Activity位于一些外部库中,不调用super,你将需要手动调用Fragment super。onRequestPermissionsResult你在你的活动onRequestPermissionsResult。

YourActivity extends SomeActivityNotCallingSuperOnRequestPermissionsResult{
Fragment requestingFragment;//the fragment requesting the permission
...
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestingFragment!=null)
            requestingFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
...

我希望它能正常工作

活动:

 ActivityCompat.requestPermissions(this,permissionsList,REQUEST_CODE);

对于片段:

 requestPermissions(permissionsList,REQUEST_CODE);

根据goodgamerguy的回答,解决方案是:

myFragment.this.requestPermissions(....)

在你根据上面的答案检查一切之前,确保你的请求代码不是0!!

检查FragmentActivity.java中onRequestPermissionsResult()的代码:

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    int index = (requestCode>>16)&0xffff;
    if (index != 0) {
        index--;

        String who = mPendingFragmentActivityResults.get(index);
        mPendingFragmentActivityResults.remove(index);
        if (who == null) {
            Log.w(TAG, "Activity result delivered for unknown Fragment.");
            return;
        }
        Fragment frag = mFragments.findFragmentByWho(who);
        if (frag == null) {
            Log.w(TAG, "Activity result no fragment exists for who: " + who);
        } else {
            frag.onRequestPermissionsResult(requestCode&0xffff, permissions, grantResults);
        }
    }
}

如果你从一个片段中调用这段代码,它有自己的requestPermissions方法。

基本概念是,如果你在一个活动中,那么调用

ActivityCompat.requestPermissions(this,
                            permissionsList,
                            permissionscode);

如果是碎片,就打电话

requestPermissions(permissionsList,
                            permissionscode);