Load intent:// url in WebView
handle Intent type url in WebView Programmatically!
Probably, you may be looking for Url's that starts with this suffix >>> intent://... and you may confused, how you can control this type of Url , Okay don't worry, i am here to fix your problem, as well as discuss and customize its behaviour.
To contorl this Url we must implement WebViewClient in our WebView Activity Like this >>>
C:\Users\Nurujjaman Pollob\Documents\NetBeansProjects\Nurujjaman Pollob\src\nurujjaman\pollob\Package.java |
1 /* 2 * Copyright 2020 Nurujjaman Pollob. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package nurujjaman.pollob; 17 18 /** 19 * 20 * @author Nurujjaman Pollob 21 */ 22 public class Package extends AppCompatActivity { 23 24 WebView web; 25 26 27 @Override 28 public void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 31 32 //Setting UP WebViewClient 33 web.setWebViewClient(new WebViewClient(){ 34 35 //We will code here to handle Intent Type Url 36 37 }); 38 39 } 40 41 42 } 43
Than Copy/Paste below Code! for your help, i have implemented more url type handle in this code
Can Compatible with all Android Version (Include 10.0)
C:\Users\Nurujjaman Pollob\Documents\NetBeansProjects\Nurujjaman Pollob\src\nurujjaman\pollob\Package.java |
1 /* 2 * Copyright 2020 Nurujjaman Pollob. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package nurujjaman.pollob; 17 18 import java.net.URISyntaxException; 19 import javax.naming.Context; 20 21 /** 22 * 23 * @author Nurujjaman Pollob 24 */ 25 public class Package extends AppCompatActivity { 26 27 WebView web; 28 29 30 @Override 31 public void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 34 35 //Setting UP WebViewClient 36 web.setWebViewClient(new WebViewClient(){ 37 38 //We will code here to handle Intent Type Url 39 40 @Override 41 public boolean shouldOverrideUrlLoading(WebView view, String url) { 42 43 44 45 46 if(url.startsWith("http://")|| url.startsWith("https://")){ 47 48 49 view.loadUrl(url); 50 51 52 } 53 54 if (url.startsWith("intent://")) { 55 try { 56 Context context = view.getContext(); 57 Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); 58 59 if (intent != null) { 60 view.stopLoading(); 61 62 PackageManager packageManager = context.getPackageManager(); 63 ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); 64 if (info != null) { 65 context.startActivity(intent); 66 } else { 67 String fallbackUrl = intent.getStringExtra("browser_fallback_url"); 68 view.loadUrl(fallbackUrl); 69 70 // or call external broswer 71 // Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fallbackUrl)); 72 // context.startActivity(browserIntent); 73 } 74 75 return true; 76 } 77 } catch (URISyntaxException e) { 78 79 Toast.makeText(MainBrowser.this, "Failed to process this intent >>> " + e,Toast.LENGTH_LONG).show(); 80 } 81 } 82 83 84 85 86 if(url.startsWith("mailto:")){ 87 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url)); 88 89 90 startActivity(intent); 91 92 } 93 94 95 if (url.startsWith("tel:")) { 96 Intent intent; 97 intent = new Intent(Intent.ACTION_DIAL, 98 Uri.parse(url)); 99 startActivity(intent); 100 } 101 102 103 return true; 104 } 105 106 107 108 @TargetApi(24) 109 public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { 110 111 web.loadUrl(request.getUrl().toString()); 112 113 if(request.getUrl().toString().startsWith("mailto:")){ 114 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(request.getUrl().toString())); 115 startActivity(intent); 116 117 } 118 119 120 if(request.getUrl().toString().startsWith("intent://")) { 121 try { 122 Context context = view.getContext(); 123 Intent intent = Intent.parseUri(request.getUrl().toString(), Intent.URI_INTENT_SCHEME); 124 125 if (intent != null) { 126 view.stopLoading(); 127 128 PackageManager packageManager = context.getPackageManager(); 129 ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); 130 if (info != null) { 131 context.startActivity(intent); 132 } else { 133 String fallbackUrl = intent.getStringExtra("browser_fallback_url"); 134 view.loadUrl(fallbackUrl); 135 136 // or call external broswer 137 // Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fallbackUrl)); 138 // context.startActivity(browserIntent); 139 } 140 141 return true; 142 } 143 } catch (URISyntaxException e) { 144 145 Toast.makeText(MainBrowser.this, "Failed to process this intent >>> " + e,Toast.LENGTH_LONG).show(); 146 } 147 } 148 149 150 if (request.getUrl().toString().startsWith("tel:")) { 151 Intent intent = new Intent(Intent.ACTION_DIAL, 152 Uri.parse(request.getUrl().toString())); 153 startActivity(intent); 154 155 156 } 157 158 159 160 if(request.getUrl().toString().startsWith("http://")|| request.getUrl().toString().startsWith("https://")){ 161 162 web.loadUrl(request.getUrl().toString()); 163 164 165 } 166 167 return true; 168 } 169 170 171 }); 172 173 } 174 175 176 } 177 178 179 180 181 182
Comments
Post a Comment
Wow nice post!