gpt4 book ai didi

java - requestPermissions() 什么都不做

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:30 25 4
gpt4 key购买 nike

问题...

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

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

我做错了什么?

详细信息...

我的 Activity 的相关部分:

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: skipping rationale

E/HomeActivity: Build version: 23

E/HomeActivity: Getting permissions

list 包括:<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 中,并且在那里工作正常。它现在可以留在那里,但如果有人知道发生了什么,我们仍然会感激不已。

最佳答案

好吧,尝试使用 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);

关于java - requestPermissions() 什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com