onRequestPermissionsResult 未在 android 活动中回调

Posted

技术标签:

【中文标题】onRequestPermissionsResult 未在 android 活动中回调【英文标题】:onRequestPermissionsResult is not called back in an android activity 【发布时间】:2017-02-28 08:41:17 【问题描述】:

我正在尝试在启动我的相机活动之前获得用户的相机许可。用户从权限对话框中按下“允许”后,永远不会回调 onRequestPermissionsResult。这是我的活动课程:

public class ImageCaptureActivity extends AppCompatActivity 

    public static final String TAG = ImageCaptureActivity.class.getSimpleName();

    private static final int REQUEST_CAMERA = 0;
    private static final int REQUEST_CAMERA_PERMISSION = 1;
    private Point mSize;

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

        Display display = getWindowManager().getDefaultDisplay();
        mSize = new Point();
        display.getSize(mSize);

        // Request for Camera Permission
        requestForCameraPermission(findViewById(android.R.id.content));
    

    /**
     * @param view
     * @brief requestForCameraPermission
     */
    public void requestForCameraPermission(View view) 
        Log.v(TAG, "Requesting Camera Permission");

        final String permission = Manifest.permission.CAMERA;
        if (ContextCompat.checkSelfPermission(ImageCaptureActivity.this, permission)
                != PackageManager.PERMISSION_GRANTED) 
            if (ActivityCompat.shouldShowRequestPermissionRationale(ImageCaptureActivity.this, permission)) 
                showPermissionRationaleDialog(getString(R.string.camera_permission_rationale), permission);
             else 
                requestForPermission(permission);
            
         else 
            launch();
        
    

    /**
     * @param message
     * @param permission
     * @brief showPermissionRationaleDialog
     */
    private void showPermissionRationaleDialog(final String message, final String permission) 
        new AlertDialog.Builder(ImageCaptureActivity.this)
                .setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                        ImageCaptureActivity.this.requestForPermission(permission);
                    
                )
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 

                    
                )
                .create()
                .show();
    

    /**
     * @param permission
     * @brief requestForPermission
     */
    private void requestForPermission(final String permission) 
        ActivityCompat.requestPermissions(ImageCaptureActivity.this, new String[]permission, REQUEST_CAMERA_PERMISSION);
    

    /**
     * @brief launch
     */
    private void launch() 
        Log.v(TAG, "Camera Permission Granted, launching the CameraActivity");
        String documentId = getIntent().getStringExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID);
        Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
        startCustomCameraIntent.putExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID, documentId);
        startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
    

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
        switch (requestCode) 
            case REQUEST_CAMERA_PERMISSION:
                final int numOfRequest = grantResults.length;
                final boolean isGranted = numOfRequest == 1
                        && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
                Log.v(TAG, "Camera Permission callback on onRequestPermissionsResult");
                if (isGranted) 
                    launch();
                
                break;

            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
    

这是我的带有相机权限的应用清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.testpackage">

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature
        android:name="android.hardware.camera.front"
        android:required="true" />
    <uses-feature android:name="android.hardware.camera2" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application

我的 Gradle 依赖项:

compile 'com.android.support:appcompat-v7:24.0.0'

compile 'com.android.support:design:24.0.0'

compile 'com.android.support:support-v4:24.0.0'

【问题讨论】:

你有in the proper location清单中的CAMERA权限吗? @CommonsWare 我已将清单信息添加到问题中。是的,我有权限。我能够获得工作许可。但是回调没有被调用。 【参考方案1】:

我自己尝试了您的代码,一切正常。我认为唯一可能成为问题的是您的活动具有以下两个标志的情况:

 android:noHistory="true"
 android:excludeFromRecents="true"

这是一个解释问题的人:https://***.com/a/35772265/5281581

【讨论】:

感谢您的尝试。删除 android:noHistory="true" 对我有用。 FLAG_ACTIVITY_NO_HISTORY 具有以下功能。 [ int FLAG_ACTIVITY_NO_HISTORY 如果设置,新活动不会保存在历史堆栈中。一旦用户离开它,活动就完成了。这也可以使用 noHistory 属性进行设置。如果设置,onActivityResult() 在当前活动开始一个设置结果并完成的新活动时永远不会调用。 ] 如果 requestPermission() 被调用,则该活动被销毁。所以无法得到结果。

以上是关于onRequestPermissionsResult 未在 android 活动中回调的主要内容,如果未能解决你的问题,请参考以下文章