gpt4 book ai didi

android - 如何在 Flutter 插件中请求权限?

转载 作者:IT王子 更新时间:2023-10-29 06:46:23 29 4
gpt4 key购买 nike

我正在尝试使用一个简单的 Flutter 插件(语音识别包装器),但不知道如何在 Android 23 或更高版本上请求适当的权限。在 Dart 部分我有:

  Future requestPermissions() => 
_channel.invokeMethod("speech.requestPermissions");

在安卓部分:

  public class SpeechRecognitionPlugin implements MethodCallHandler, RecognitionListener,
PluginRegistry.RequestPermissionResultListener {

插件注册:

  public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "speech_recognition");
SpeechRecognitionPlugin speechRecognitionPlugin = new
SpeechRecognitionPlugin(registrar.activity(), channel);
channel.setMethodCallHandler(speechRecognitionPlugin);
registrar.addRequestPermissionResultListener(speechRecognitionPlugin);
}

方法调用:

else if (call.method.equals("speech.requestPermissions")) {
Log.d(LOG_TAG, "speech.requestPermissions");
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(activity.getApplicationContext(), "This application needs the Record Audio permission for recognition to work", Toast.LENGTH_LONG).show();
} else {
Log.d(LOG_TAG, "Requesting permissions");
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.RECORD_AUDIO},
1);
}
result.success(hasRecordAudioPermission());

结果回调:

@Override                                                                                            
public boolean onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
boolean granted = false;
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
granted = true;
}
speechChannel.invokeMethod("speech.onPermission", granted);
return true;
}
}
return false;
}

从 logcat 我看到发生了“speech.requestPermissions”调用,但是没有显示标准的 Android 系统权限请求,只是 logcat 中的这个可能是相关的:

D/ViewRootImpl(21171): #1 mView = android.widget.LinearLayout{64f050b 
V.E...... ......I. 0,0-0,0 #102039d android:id/toast_layout_root}
D/ViewRootImpl(21171): MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl(21171): #3 mView = null

Flutter 插件请求权限的正确方式是什么?

编辑:这不适用于对话框正确显示时的第一次运行,但适用于用户最初未授予权限或通过设置撤销权限时的后续运行。我意识到这会显着改变问题(使其显示为边缘案例),但 Android 权限不应该以这种方式工作。

编辑:权限存在于 AndroidManifest.xml 中

最佳答案

使用Permission plugin for flutter

请求许可
import 'package:permissions_plugin/permissions_plugin.dart';

Map<Permission, PermissionState> permission = await PermissionsPlugin
.requestPermissions([
Permission.ACCESS_FINE_LOCATION,
Permission.ACCESS_COARSE_LOCATION,
Permission.READ_PHONE_STATE
]);
检查状态权限
import 'package:permissions_plugin/permissions_plugin.dart';

Map<Permission, PermissionState> permission = await PermissionsPlugin
.checkPermissions([
Permission.ACCESS_FINE_LOCATION,
Permission.ACCESS_COARSE_LOCATION,
Permission.READ_PHONE_STATE
]);

关于android - 如何在 Flutter 插件中请求权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858434/

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