gpt4 book ai didi

java - 应用程序崩溃而不显示权限对话框,但在手动授予权限时工作正常

转载 作者:行者123 更新时间:2023-12-02 10:07:34 25 4
gpt4 key购买 nike

我对 Android 开发还比较陌生。我有一个需要某些权限的应用程序。这些被添加到 AndroidManifest.xml 中:

    <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.STORAGE"/>
<uses-permission android:name="android.permission.PHONE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

应用在首次启动时崩溃(在模拟设备和已安装的 apk 上均如此),但在手动授予相机电话存储权限后从手机设置来看,效果很好。

MainActivity中,我还添加了如下权限检查(在onCreate中):

 // CAMERA permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
1);
}

但是应用程序在启动时不断崩溃,而不显示权限对话框。我在这里做错了什么?

编辑错误跟踪:

enter image description here

最佳答案

您可以尝试下面的代码,这对我有用。它用于相机和存储。调用此方法 checkAndRequestPermissions();

private void checkAndRequestPermissions() {
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};

if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}


public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//granted
} else {
//not granted

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
showDialogOK(getString(R.string.app_permission_allow),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
showDialogOK(getString(R.string.app_permission_necessary),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
finish();
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
break;
}
}
});


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

关于java - 应用程序崩溃而不显示权限对话框,但在手动授予权限时工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55232364/

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