在Android应用程序中授予WebView + webRTC权限的问题

The problem to grant permissions in WebView+webRTC in Android app

提问人:Sagamor Nord 提问时间:1/24/2020 最后编辑:Sagamor Nord 更新时间:6/18/2021 访问量:3868

问:

在Android设备中将Chrome更新到79 ver. 2019.12.18后,问题出现在我的WebView中。我的 WebView 停止请求提供对摄像头和麦克风的访问权限,尽管在早期的 Chrome 版本(56、78)中它起作用了。我使用了那个 webRTC 提供程序:https://trueconf.ru/。我每 15 秒就会在日志中出现错误:

E/chromium: [ERROR:web_contents_delegate.cc(218)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.

如果我尝试在 Chrome 中打开 webRTC-session,它就可以工作了。所以,我认为,Chrome 和 Chromium 之间存在一些分歧,或者类似的东西。 我创建了一个小应用程序,模拟了我的原始代码:

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private static final int RC_CAMERA_AND_VOICE = 1;
    private static final int RC_CAMERA_AND_CAMERA = 2;
    private static final int RC_CAMERA_AND_CAPTURE = 3;


    WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermissions();
    }

    private void checkPermissions() {

        Log.e("VideoChat", "checkPermissions");


        if ((ContextCompat.checkSelfPermission(
                this.getApplication(), 
                Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) &&
                (ContextCompat.checkSelfPermission(this.getApplication(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)) {

            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},0);
        } else {
            this.startWebView();
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        }
        this.startWebView();
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void startWebView() {
        webView = findViewById(R.id.web_view);

        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                Log.e("VideoChat", "onPermissionRequest");
                request.grant(request.getResources());
            }

        });

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(webView, true);

        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSaveFormData(true);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setDomStorageEnabled(true);

        webView.loadUrl("https://your.webrtc.provider");
    }
}

我的清单 .xml:

<uses-permission android:name="android.webkit.PermissionRequest" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
    <uses-permission android:name="android.permission.WRITE_LOCAL_STORAGE" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:ignore="ProtectedPermissions"
        tools:node="replace"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.MODIFY_AUDIO_SETTINGS"
        tools:ignore="ProtectedPermissions"
        tools:node="replace"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.RECORD_AUDIO"
        tools:ignore="ProtectedPermissions"
        tools:node="replace"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.CAMERA"
        tools:ignore="ProtectedPermissions"
        tools:node="replace"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.CAPTURE_VIDEO_OUTPUT"
        tools:ignore="ProtectedPermissions"
        tools:node="replace"
        tools:remove="android:maxSdkVersion" />
    <uses-permission
        android:name="android.permission.CAPTURE_AUDIO_OUTPUT"
        tools:ignore="ProtectedPermissions"
        tools:node="replace"
        tools:remove="android:maxSdkVersion" />
<uses-feature
        android:name="android.hardware.camera.front"
        android:required="true" />
    <uses-feature
        android:name="android.hardware.microphone"
        android:required="true" />

需要知道:我没有注意到其他 webRTC 提供商的这个问题,但我必须使用我的提供商。请提供任何帮助。

Android WebView

评论

3赞 Daniel RL 1/29/2021
你找到解决方案了吗?

答: 暂无答案