Android 在录音后获取访问麦克风的权限提示

Android 在录音后获取访问麦克风的权限提示,android,android-studio,Android,Android Studio,我正在建立一个应用程序,记录用户的语音,然后将其转换为文本格式。我试图在用户单击录制按钮后立即获得访问麦克风的权限提示。但是在演讲被录音后,我得到了许可提示。我错在哪里 以下是java和xml文件: MainActivity.java package com.ika.speechtotext; import android.Manifest; import android.content.ActivityNotFoundException; import android.content.Int

我正在建立一个应用程序,记录用户的语音,然后将其转换为文本格式。我试图在用户单击录制按钮后立即获得访问麦克风的权限提示。但是在演讲被录音后,我得到了许可提示。我错在哪里

以下是java和xml文件:

MainActivity.java

package com.ika.speechtotext;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.speech.RecognizerIntent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity{

    private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (TextView)findViewById(R.id.myText);
    }

    public void onBtnClick(View view) {


        if (view.getId() == R.id.imageRecBtn) {

            // Here, 'this' is the current activity i.e; MainActivity
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.READ_CONTACTS)
                    != PackageManager.PERMISSION_GRANTED) {

                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.RECORD_AUDIO)) {

                    // Show an explanation to the user *asynchronously* -- don't block
                    // this thread waiting for the user's response! After the user
                    // sees the explanation, try again to request the permission.

                } else {

                    // No explanation needed, we can request the permission.

                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.RECORD_AUDIO},
                            MY_PERMISSIONS_REQUEST_RECORD_AUDIO);

                    // MY_PERMISSIONS_REQUEST_RECORD_AUDIO is an
                    // app-defined int constant. The callback method gets the
                    // result of the request.
                }
            }

            promptSpeechInput();
        }
    }

    //recognize the speech
    public void promptSpeechInput() {
        Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
        i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak something!");

        try {
            startActivityForResult(i, 100);
        } catch (ActivityNotFoundException a) {
            Toast.makeText(MainActivity.this, "Your device doesn't support speech recognition.",
                    Toast.LENGTH_SHORT).show();
        }
    }

    //display the speech in text format
    public void onActivityResult (int request_code, int result_code, Intent i) {
        super.onActivityResult(request_code, result_code, i);

        switch (request_code) {

            case 100:
                if (result_code == RESULT_OK && i != null) {
                    ArrayList<String> result = i.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    text.setText(result.get(0));
                }
                break;
        }
    }


    //a toast for fun
    public void onTextClick(View view){
        Toast.makeText(MainActivity.this, "You are a Genius! ;-)", Toast.LENGTH_SHORT).show();
    }

}

我现在没有我的工作电脑,所以我不能发布代码,但我会告诉你操作步骤

  • 用户单击“记录”按钮时,检查是否授予记录权限。如果是,则调用promptSpeechInput()方法,否则请求记录权限

  • 重写活动中的方法。在此检查requestCode是否等于您请求的记录权限的requestCode。如果是,则检查是否授予了权限。如果被授予,则调用promptSpeechInput()方法,否则不执行任何操作或返回或退出。不需要理由,因为要求录音许可来录制音频显然是可以理解的。 我希望这是清楚的。请随时提出与步骤相关的任何问题


  • 代码中有几个问题与您看到的问题有关:

  • 无论您的应用程序是否已被授予录制音频权限,它仍会调用
    promptSpeechInput()
    。除非您获得许可,否则不应发生这种情况。这意味着只有在此处授予了
    权限时,或者为请求调用了
    onRequestPermissionResult()
    时,才应该调用它
  • 应用程序正在检查
    读取联系人
    ,然后请求
    录制音频
  • 如果应用程序以前从未请求过权限,则
    ActivityCompat.shouldShowRequestPermissionRegulation()
    的结果可能不是您期望的结果。只有在应用程序请求并被拒绝权限后,才应使用此方法。它为您提供了有关是否应通知用户为什么需要该权限(以及他们是否希望被告知该权限)的信息
  • 较新的权限模型可能很难使用。您可能会发现这篇关于新车型的演讲很有帮助:


    您还可能发现此权限帮助器库更易于使用。它将在建议的条件下处理用户提示,并在应用程序具有权限时调用相关的“受保护”代码:

    似乎,您正在检查另一个权限:Manifest.permission.READ_CONTACTS@DmitryArc是的,您正在寻找READ_联系人,如果您试图检查音频权限是否被授予,但是你不能输入IF语句。谢谢你的回答。教程链接非常有用。我修复了权限提示问题,但我希望应用程序在授予权限后立即开始录制语音(现在我必须再次单击rec按钮)。提供了更新的“MainActivity.java”代码段。您仍然需要添加一个
    onRequestPermissionsResult()
    回调,如果授予了该权限,则调用
    promptSpeechInput()
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.ika.speechtotext">
    
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/myTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    public void onBtnClick(View view) {
    
    
        if (view.getId() == R.id.imageRecBtn) {
    
            //New code lines BEGIN***************************************************************************************************
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //checking the permission status
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) !=
                        PackageManager.PERMISSION_GRANTED) {
                    //request the permission
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.RECORD_AUDIO},
                            REQUEST_CODE_RECORD_AUDIO);
    
                } else {
    
                    promptSpeechInput();
                }
            }
            //New code lines END***************************************************************************************************
    
        }
    
    
    }