Method of clicking hyperlink to start QQ in WebView in Android

preface

Once in an interview, the interviewer asked how to click a hyperlink in WebView to start a type of QQ similar to a third-party application. My answer at that time was that it can be done by interacting with JS with WebView. The interviewer didn't say anything. It should be that the answer is not what he expected. Today, I found that it can be realized in this way. Record it.

Realization idea

In web development, you can start a QQ temporary session through a URL link

Replace 20178888 with your QQ number. This can also be called normally in some browsers of Android phones.

But if we want to use WebView to display the web page, the page contains the above link, but we can't start QQ normally.

We can overload the setwebviewclient method in WebView

In this way, if the hyperlink in WebView is triggered, WebView will load instead of the default browser.

This link looks like a normal HTTP request. Through packet capturing, it is found that the QQ called is not only an HTTP request, but actually sent such a request: mqqwpa://im/chat At the beginning.

Mqqwpa: / / this part of the URL is called the sechme part of the URL.

It's easy to understand why I jumped to the official website of Tencent app treasure, http://wpa.qq.com/ It is the official website of Tencent app Bao. This is the first request, because the protocol it requests again is not HTTP, but mqqwpa://im/chat 。

In fact, what really calls QQ is mqqwpa://im/chat At this time, we should use an intent to operate and call the native QQ.

We need to capture these URLs. When we encounter ordinary web pages, we need to submit them to WebView for processing and follow the normal process; In case of problems starting with mqqwpa, it shall be handled by the default browser. In this way, the problem of WebView clicking on the hyperlink to start QQ is solved.

Let's look at the implementation:

In fact, we can overload shouldinterceptrequest method to intercept other sechme processing.

Full code:

Operation effect

summary

The above is the whole content of this article. I hope the content of this article can bring some help to Android developers. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>