gpt4 book ai didi

android - 在 webview 上共享 whatsapp

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:51 25 4
gpt4 key购买 nike

我有 android webview 应用程序,并且在网络源中可以将新闻内容共享到 whatsapp。它在移动浏览器中运行完美,但在 Web View 中不起作用。

它的显示文本 whatsapp://send?text=Hello world

这是我的代码

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();
}
}

最佳答案

将以下代码添加到您的 WebViewClient() 方法中,它将完美运行

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;
}
}

关于android - 在 webview 上共享 whatsapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39989428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com