Android Addjavascriptinterface not effective solution

Source: Internet
Author: User

1. Webview.addjavascriptinterface () Doen not work on API 16+

Common client-to-H5 programming is the use of scheme+, JS Bridge, provider and so on.

When doing Android and H5 client interaction, found a very strange error. On a 4.2.2 Samsung S3, there is a callback error in Android Javascript Bridge.

All functions that are hanging under bridge objects cannot be found. So a little bit to troubleshoot where the problem actually appears.

Other mobile phone H5 mixed programming message callback normal, this thought is because the travel V3.0 is based on fragment, that is, fragment can not be used. It was not the problem that had been discovered later.

Discovery is the hand of a S3 no, the other basic can be a message callback.

Troubleshooting a large circle, found in the Android SDK Target API level 16+ in the case of native in the previous way and H5 mixed programming is not done.

Workaround:

    1. It is recommended to use the higher target SDK to compile "4.0+".

2. All callback methods under the JavaScript Bridge object use @JavascriptInterface for annotation declaration

3. import android.webkit.JavascriptInterface required;

using 4.0+ version of the target SDK Note that the callback method should be annotated! This kind of pit is hard to find, and the team with similar hybrid programming techniques, please pay attention to this ~

Device ROM version

Development environment Target SDK

Whether the message will take effect without annotations

<4.0

>4.0

Ok

<4.0

<4.0

Ok

>4.0

>4.0

NO

<4.0

>4.0

Ok

public class J2nbridgemanager {

@JavascriptInterface

Public void startnativeservice (String schemeurl) {

JS callback is empty then return directly

if (Textutils. IsEmpty (Schemeurl)) {

return;

}

Final Tripurl Mserviceurl = Utils. geturibyurlwithdecoded (Schemeurl);

Executes a local service, after the asynchronous processing, callback JS

Mhandler.post ( new Runnable () {

@Override

Public void Run () {

Doservice (Mserviceurl);

}

});

}

}

Reference article:

How to establish communication: http://stackoverflow.com/questions/13063222/web-view-addjavascriptinterface-issue

Problem-Solving Description: HTTP://STACKOVERFLOW.COM/QUESTIONS/16353430/APPVIEW-ADDJAVASCRIPTINTERFACE-DOEN-NOT-WORK-ON-API-17

2.android 5.0+ version of message sharing, unable to evoke third-party clients through scheme

We will check whether the phone installs the travel client by provider in WebView and then logically distribute it in the following way:

Wake-up of third-party clients via scheme can be achieved by window.location.href = ' taobaotravel://h5_homepage ', before the <5.0 version, by a custom scheme However, after the 5.0 version, it seems that the URL filter in the WebView, corresponding to the non-trusted URL is masked, so the scheme can not be initiated to wake the client.

About the URL whitelist this thing, at present just for speculation, if who has a friend over there "research and development team is in Guangzhou", can help confirm under.

If the confirmation is whitelist, the current third-party app that wakes us through 5.0+webview will turn into a dead end ....

It is also desirable to be able to work through business

if (ISMAC) {
if (getavailabled ()) {
$ (' j_btn '). InnerHTML = ' travel start ... ';
SetTimeout (function () {
if (Window.location.href.match (' promo-assit.php ')) {
window.location.href = ' taobaotravel://h5_homepage ';
$ (' j_btn '). InnerHTML = ' Start Travel client ';
Return
}
window.location.href = ' taobaotravel://h5_webview ' + param;
$ (' j_btn '). InnerHTML = ' Start Travel client ';
}, 1300);
} else {
Jump Activity Page
if (Window.location.href.match (' promo-assit.php ')) {
window.location.href = ' http://lv.taobao.com/';
} else {
Window.location.href = Fixurl (Jmpurl);
}
}
}

Android Addjavascriptinterface not effective solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.