I have android webview app and in the web source have sharing news content to whatsapp. It's works perfect from mobile browser but its doesn't work from web view.
its show text whatsapp://send?text=Hello world
this is my code
package com.web.viewers;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.daimajia.numberprogressbar.NumberProgressBar;
public class MainActivity extends AppCompatActivity {
private WebView webView;
private String url = "http://www.myurl.com";
private NumberProgressBar progressBar;
private String TAG = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeWebView();
}
@SuppressLint("SetJavaScriptEnabled")
private void initializeWebView() {
if (getIntent().getStringExtra("url") != null){
url = getIntent().getStringExtra("url");
}
Log.d(TAG, "initializeWebView: " + url);
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
progressBar = (NumberProgressBar)
findViewById(R.id.number_progress_bar);
progressBar.setVisibility(View.GONE);
}
public class WebChromeClient extends android.webkit.WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
}
public class WebViewClient extends android.webkit.WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view,
WebResourceRequest request) {
return super.shouldOverrideUrlLoading(view, request);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
}
@Override
public void onBackPressed() {
if (webView.canGoBack()){
webView.goBack();
} else {
showAlertDialog();
}
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage("Tutup Aplikasi ini ?")
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.
WebView is in common use in Android applications. Although default configuration is secure, developers tend to introduce changes in its configuration which may introduce security risks.
Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.
This interface was deprecated in API level 12. This interface is now obsolete.
Add this below code in your WebViewClient()
method and it will be working perfectly
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
It looks like if you are clicking a link with a custom scheme from a WebView on Android, that you now need to implement a handler in the WebViewClient.
To get this working, you need to update your implementation of WebViewClient.shouldOverrideUrlLoading
to something like the following:
@Override
public boolean shouldOverrideUrlLoading(WebView view,
WebResourceRequest request) {
Uri uri = request.getUrl();
if (Objects.equals(uri.getScheme(), "whatsapp")) {
try {
Intent intent = Intent.parseUri(request.getUrl().toString(), Intent.URI_INTENT_SCHEME);
if(intent.resolveActivity(getPackageManager()) != null)
startActivity(intent);
return true;
} catch (URISyntaxException use) {
Log.e(TAG, use.getMessage());
}
}
return super.shouldOverrideUrlLoading(view, request);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With