问题...
我正在尝试在 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,它在那里工作正常。它现在可以留在那里,但如果有人知道发生了什么,输入仍然很感激。