Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

{SOLVED} Error Unknown Url Scheme in Android Webview

{SOLVED} Error Unknown Url Scheme in Android Webview





Today in this post, we will solve an error when we try to open a url in webview which wants to open a specific app. For Eg. when we open instagram.com in our android webview, there's a button of open in app, when we click on that button it will show us an error of unknown url scheme. we will solve this error in this post.

To create a full WebView click here 

  • There are two methods of shouldoverrideurlloading in android webview webclient.
  • We will use the method which has webresourcerequest and Webview arguments.
  • shouldoverriddeurlloading method is called when we click on a button or Url in android webview
  • We will start activity intent on a button click
  • When url starts with intent:// , whatsapp:// etc. we should have handle clicking that url
  • We can put conditions of checking url with these keywords or start url without conditions.
Java code with full webclient, see that method carefully
 mywebview.setWebViewClient(new WebViewClient() {  
       @Override  
       public void onPageStarted(WebView view, String url, Bitmap favicon) {  
         progressbar.setVisibility(View.VISIBLE);  
         tooltext.setText(mywebview.getUrl());  
         invalidateOptionsMenu();  
         final String Urls = url;  
   
         if (Urls.contains("mailto:") || Urls.contains("sms:") || Urls.contains("tel:")) {  
           mywebview.stopLoading();  
           Intent i = new Intent();  
           i.setAction(Intent.ACTION_VIEW);  
           i.setData(Uri.parse(Urls));  
           startActivity(i);  
         }  
   
         super.onPageStarted(view, url, favicon);  
       }  
   
       @Override  
       public void onPageFinished(WebView view, String url) {  
         progressbar.setVisibility(View.GONE);  
         invalidateOptionsMenu();  
   
         addhistory();  
   
         tooltext.setText(mywebview.getUrl());  
         super.onPageFinished(view, url);  
       }  

//This code is used to handle url scheme. See the method carefully

       @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  
       @Override  
       public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {  
   
         mywebview.loadUrl(String.valueOf(request.getUrl()));  
   
         //if you want to open a specific activity from webview  
         if( String.valueOf(request.getUrl()).startsWith("whatsapp://"))  
         {  
           mywebview.stopLoading();  
   
           try {  
             Intent intent = new Intent(Intent.ACTION_SEND);  
             intent.setType("text/plain");  
             intent.setPackage("com.whatsapp");  
   
             intent.putExtra(Intent.EXTRA_TEXT,mywebview.getUrl());  
             startActivity(intent);  
           }  
           catch(Exception e){  
   
         }  
         }  
   
         //if you want to open an activity chooser  
         // our url scheme starts with intent, if app found then it opens that, otherwise it catch exception only  
         try {  
           Intent intent = Intent.parseUri(String.valueOf(request.getUrl()),Intent.URI_INTENT_SCHEME);  
           startActivity(intent);  
           //This works when you open any app, but if you open specific app then  
         }  
         catch(Exception e)  
         {  
   
         }  
   
         return true;  
       }  
   

       @Override  
       public boolean shouldOverrideUrlLoading(WebView view, String url) {  
   
         Log.d("TAG", "shouldOverrideUrlLoading: 3");  
         
           Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
           startActivity( intent );  
           
         return true;  
       }  
     });  



 
Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments