When integrating a WebView with WebRTC into your native application, there are several key elements and permissions that need to be declared in the AndroidManifest.xml file.
Permissions
Permissions are declared in the manifest file using the <uses-permission> element. For a WebView with WebRTC, you typically need to declare the following permissions:
INTERNET: Allows your application to open network sockets. This is necessary for the WebView to load web content.
MODIFY_AUDIO_SETTINGS: It's needed for WebRTC to control the audio settings for optimal communication. Pay additional attention, that only RECORD_AUDIO permissions are not enough to let application properly using audio channels
This element is used to declare hardware or software features used by the application, which can affect whether or not the application can be installed on a device. For a WebView with WebRTC, you typically need to declare the following feature:
android.hardware.camera: Indicates that the application uses the device's camera
Replace YourAppTheme and MainActivity with the theme and activity names used in your application.
In the MainActivity check for camera and record audio permissions first. The REQUEST_CAMERA_MIC_PERMISSION constant is used as a request code for permission requests. If permissions are not granted then WebView will not work as expected.
The onRequestPermissionsResult method handles the result of the permission request. If the permissions are granted, the app can proceed with accessing the camera and microphone. If not, the app should handle the denial appropriately.
In the onCreate method, the WebView is configured. JavaScript is enabled, DOM storage is enabled, and media playback does not require user gestures. A WebChromeClient is set to handle permission requests within the WebView. Finally, the WebView loads a specific URL.
MainActivity
publicclassMainActivityextendsAppCompatActivity {privatestaticfinalint REQUEST_CAMERA_MIC_PERMISSION =123;privateWebView webView =null; @OverrideprotectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// Check if the permissions are grantedif (ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.CAMERA) !=PackageManager.PERMISSION_GRANTED||ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.RECORD_AUDIO) !=PackageManager.PERMISSION_GRANTED) {// If not, request the permissionsActivityCompat.requestPermissions(getActivity(),newString[]{Manifest.permission.CAMERA,Manifest.permission.RECORD_AUDIO}, REQUEST_CAMERA_MIC_PERMISSION); } else {// If grantedinit()// initiate WebView and load url } } @OverridepublicvoidonRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {if (requestCode == REQUEST_CAMERA_MIC_PERMISSION) {if (grantResults.length>0&& grantResults[0] ==PackageManager.PERMISSION_GRANTED&& grantResults[1] ==PackageManager.PERMISSION_GRANTED) {// Permissions granted initiate WebView and load urlinit() } else {// Permissions deniedfinish()// close the application } } } @OverrideprotectedvoidonDestroy() { super.onDestroy();// Clear WebViewif(webView !=null) {webView.loadUrl("about:blank") webView =null } } @SuppressLint("SetJavaScriptEnabled") privatevoidinit() { webView =findViewById(R.id.web_view);WebSettings webSettings =webView.getSettings();// Enable JavaScriptwebSettings.setJavaScriptEnabled(true);// Enable DOM storage APIwebSettings.setDomStorageEnabled(true);// Allow video autoplaywebSettings.setMediaPlaybackRequiresUserGesture(false);// Set a WebChromeClient that handles permission requestswebView.setWebChromeClient(newWebChromeClient() { @OverridepublicvoidonPermissionRequest(finalPermissionRequest request) {getActivity().runOnUiThread(newRunnable() { @Overridepublicvoidrun() {request.grant(request.getResources()); } }); } });webView.loadUrl("<<url to the relevant template>>"); }}