在webview中打开whatsapp //:

时间:2019-12-30 00:44:16

标签: android android-webview

我需要在单击任何电话号码时打开whatsapp聊天。我有以下代码,但该应用程序关闭了,但我做不到 你能帮助我吗?谢谢

  myWebView.setWebViewClient(new WebViewClient(){
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    boolean overrideUrlLoading = false;

    if( url.startsWith("http:") || url.startsWith("https:") ) {
        return false;
    }

    if( url.startsWith("tel:") || url.startsWith("mailto:") ||(url != null && url.startsWith("whatsapp://"))  ) {
        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        overrideUrlLoading = true;


        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
    }
    return true;

 }
 });
myWebView.loadUrl("https://midominio.es");

enter image description here

android.content.ActivityNotFoundException:未找到用于处理Intent的活动{act = android.intent.action.VIEW dat = whatsapp:// 617560187}         在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2051)         在android.app.Instrumentation.execStartActivity(Instrumentation.java:1709)         在android.app.Activity.startActivityForResult(Activity.java:5192)         在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)         在android.app.Activity.startActivityForResult(Activity.java:5150)         在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)         在android.app.Activity.startActivity(Activity.java:5521)         在android.app.Activity.startActivity(Activity.java:5489)         在es.shop.shopyshop.principal $ 2.shouldOverrideUrlLoading(principal.java:64)         在android.webkit.WebViewClient.shouldOverrideUrlLoading(WebViewClient.java:83)         在Dx.a(PG:1)         在ic.a(PG:22)         在org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(PG:152)         在android.os.MessageQueue.nativePollOnce(本地方法)         在android.os.MessageQueue.next(MessageQueue.java:336)         在android.os.Looper.loop(Looper.java:174)         在android.app.ActivityThread.main(ActivityThread.java:7356)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

1 个答案:

答案 0 :(得分:0)

您应该注意,如果没有符合您意图的应用程序,startActivity()会抛出ActivityNotFoundException,这是您遇到的未捕获异常会终止您的应用程序的情况:

  

由于找不到活动执行给定的Intent导致对Context#startActivity或其变体之一的调用失败时,抛出此异常:

用通常的try/catch包装。

try {
    startActivity(intent);
} catch (Exception e) {
   // handle error case
} 

此外,logcat中的崩溃几乎没有任何关联