1

问题...

我正在尝试在 Android Marshmallow 中实现运行时权限,同时保持向后兼容性。我以前成功地做到了这一点,我不确定这次有什么不同。我已经在物理 Note 5(运行 Mallow)上对其进行了测试,并使用在 Marshmallow 上设置的模拟器,两者都不起作用。

我意识到“它不起作用”并不是很有帮助,但我不知道还能说什么,什么也没发生。该应用程序不会崩溃,它只是在requestPermissions(perms, 222)被调用后挂起。

我究竟做错了什么?

细节...

我活动的相关部分:

public class HomeActivity extends Activity implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_home_scancertificate:
                if (ContextCompat.checkSelfPermission(HomeActivity.this, "android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED){
                    showNoPermDialog();
                }else {
                    AppData.ActionType = ActionType.SCAN_CERTIFICATE;
                    intent = new Intent(HomeActivity.this, CaptureActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        Log.e("HomeActivity", "Permissions results...");

        switch (requestCode) {
            case 222: {

                boolean granted = (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
                Log.e("HomeActivity", granted?"granted permission":"denied permission");

                AppData.ActionType = ActionType.SCAN_POSTCARD;
                intent = new Intent(HomeActivity.this, CaptureActivity.class);
                startActivity(intent);
            }
        }
    }

    public void getCamPerm(){
        Log.e("HomeActivity", "Build version: "+Build.VERSION.SDK_INT);
        if (Build.VERSION.SDK_INT >= 23) {
            Log.e("HomeActivity", "Getting permissions");

            String[] perms = new String[]{Manifest.permission.CAMERA};
            requestPermissions(perms, 222);
        }
    }

    public void showNoPermDialog(){
        if (Build.VERSION.SDK_INT >= 23) {
            boolean showRationale = shouldShowRequestPermissionRationale(Manifest.permission.CAMERA);

            String status = showRationale ? "showing rationale" : "skipping rationale";
            Log.e("HomeActivity", status);

            if (showRationale) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
                alertDialogBuilder.setTitle("Need permission");
                alertDialogBuilder
                    .setMessage("App requires camera permission for the use of the scanner.")
                    .setCancelable(false)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                            getCamPerm();
                        }
                    });
                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }else getCamPerm();

        }else {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("Need permission");
            alertDialogBuilder
                    .setMessage("App requires permission to use the camera. You have disabled camera permission. Please re-enable this permission thru Settings -> Apps -> Our Town -> Permissions.")
                    .setCancelable(false)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    }

}

运行时,仅记录这些(onRequestPermissionsResult()根本不调用)

E/HomeActivity:跳过理由

E/HomeActivity:构建版本:23

E/HomeActivity:获取权限

清单包括:<uses-permission android:name="android.permission.CAMERA" />

编辑

仅在我第一次单击该按钮时,它才会显示在 Logcat 中,并且似乎是由调用触发的shouldShowRequestPermissionRationale()

08-17 11:26:36.996 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
08-17 11:26:36.996 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
08-17 11:26:36.996 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }
08-17 11:26:36.997 20660-20660/com.ourtownamerica.ourtowntrutrak W/ViewRootImpl: Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=1035.6592, y[0]=770.2344, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=8508970, downTime=8506643, deviceId=0, source=0x1002 }

编辑 2

我从它所在的 Activity 中复制了相同的原始代码并将其移至启动 Activity,它在那里工作正常。它现在可以留在那里,但如果有人知道发生了什么,输入仍然很感激。

4

3 回答 3

0

好吧,尝试使用 Manifest.permission.CAMERA 而不是“android.permission.CAMERA”

还用于代码质量检查按钮中的 android 是否为 23 ,单击自身R.id.btn_home_scancertificate:

if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(activity,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        showNoPermDialog();

    } else {
        AppData.ActionType = ActionType.SCAN_CERTIFICATE;
                intent = new Intent(HomeActivity.this, CaptureActivity.class);
                startActivity(intent);
    }

    //in alertdialog button simply place this for requesting permission
    ActivityCompat.requestPermissions(activity,
            new String[]{Manifest.permission.CAMERA},
            222);
于 2016-08-17T14:39:00.277 回答
0

对于 M 权限,我一直在使用此答案中的以下类:

public class MarshmallowPermission {
    public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
    public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;

    public MarshmallowPermission() {
    }

    public boolean checkPermissionForExternalStorage(Activity activity) {
        if(Build.VERSION.SDK_INT >= 23) {
            int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if(result == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    public boolean checkPermissionForCamera(Activity activity) {
        if(Build.VERSION.SDK_INT >= 23) {
            int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
            if(result == PackageManager.PERMISSION_GRANTED) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    public void requestPermissionForExternalStorage(Activity activity) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            Toast.makeText(activity,
                    "External Storage permission needed. Please allow in App Settings for additional functionality.",
                    Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

    public void requestPermissionForCamera(Activity activity) {
        if(ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) {
            Toast.makeText(activity,
                    "Camera permission needed. Please allow in App Settings for additional functionality.",
                    Toast.LENGTH_LONG).show();
        } else {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
        }
    }
}

像这样工作:

        if(marshmallowPermission.checkPermissionForCamera(activity)) {
            // has camera
        } else {
            marshmallowPermission.requestPermissionForCamera(activity);
        }

并在Activity回调中处理

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    // handle request code
}
于 2016-08-17T15:40:16.753 回答
-1

麻烦可能源于shouldShowRequestPermissionRationale()在请求许可之前打电话。对此(此处)记录了一个错误,这尤其糟糕,因为谷歌的一些示例/教程遵循这种模式。

要解决它,请检查权限,如果您尚未获得权限,请请求权限。在onRequestPermissionsResult()检查中,看看你是否应该说明理由。true只有当用户拒绝了权限并且他们没有选择不再被询问的选项时,这才会返回。

通过使用Andele库,您可能会发现使用此指南和推荐的 UX 指南更容易。

于 2016-08-17T18:41:11.857 回答