gpt4 book ai didi

javascript - 如何 Intent 在Webview中打开Whatsapp?

转载 作者:行者123 更新时间:2023-11-29 22:56:53 26 4
gpt4 key购买 nike

我想将Whatsapp用作客户服务。我的应用程序是webview,当客户进入联系页面时,会看到一个whatsapp和“聊天我们”按钮以与客户服务聊天。

当客户单击按钮时,他们没有直接打开whatsapp并显示ERR_UNKOWN_SCHEME_URL
我正在使用webview在我的应用程序中使用android studio和kotlin

这是我的 Activity :

class MainActivity : AppCompatActivity() {
internal var webView: WebView? = null
private var mCM: String? = null
private var mUM: ValueCallback<Uri>? = null
private var mUMA: ValueCallback<Array<Uri?>>? = null


@SuppressLint("SetJavaScriptEnabled", "WrongViewCast")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

webView = findViewById(R.id.ifView) as WebView
assert(webView != null)
val webSettings = webView!!.settings
webSettings.javaScriptEnabled = true
webSettings.allowFileAccess = true

if (Build.VERSION.SDK_INT >= 21) {
webSettings.mixedContentMode = 0
webView!!.setLayerType(View.LAYER_TYPE_HARDWARE, null)
}
else {
webView!!.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
}
webView!!.webViewClient = Callback()
webView!!.loadUrl("http://example.com") //add your test web/page address here
webView!!.setWebChromeClient(object : WebChromeClient() {

SuppressWarnings("deprecation")
@Override
fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean
{
if (url != null && url.startsWith("whatsapp://")) {
val sendIntent = Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Moon TV");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent)
return true
/* ContextCompat.startActivity(sendIntent);return true;*/

} else {
return false;
}
}

这是我的 Callback()代码:
inner class Callback : WebViewClient() {
override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
Toast.makeText(applicationContext, "No Internet Access!", Toast.LENGTH_SHORT).show()
view.loadUrl("file:///android_asset/error.html")
}
}

这是打开直接聊天的php代码 Whatsapps客户服务
    <a href="https://api.whatsapp.com/send?phone=628118906771&amp;text=Halo%20Example," class="btn btn-success btn-md">Visit WhatsApp</a>

我想要在Webview中使用Intent WhatsApp。谢谢。

最佳答案

从您的MyActivity中,删除以下内容:

SuppressWarnings("deprecation")
@Override
fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean
{
if (url != null && url.startsWith("whatsapp://")) {
val sendIntent = Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Moon TV");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent)
return true
/* ContextCompat.startActivity(sendIntent);return true;*/

} else {
return false;
}

然后,将此代码添加到 Callback()函数或 inner class Callback中:
override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {

var overrideUrlLoading = false

if (url != null && url.startsWith("whatsapp://")) {

view.context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))

overrideUrlLoading = true

} else {

view.loadUrl(url)
}

return overrideUrlLoading
}

关于javascript - 如何 Intent 在Webview中打开Whatsapp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353472/

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