javascript - H5頁面如何跟APP應用互動?

來源:互聯網
上載者:User
做一個活動,在app裡嵌入一個H5頁面。

如何讓使用者點擊H5頁面上的按鈕跳轉到APP的另一個頁面?

比如說點擊H5頁面上的儲值按鈕,跳轉到儲值介面,點擊立即搶購按鈕,跳轉到商品頁面。

H5與APP端是如何進行資料互動的?

後台該如何寫?

有沒有例子?

請大牛之支招。

回複內容:

做一個活動,在app裡嵌入一個H5頁面。

如何讓使用者點擊H5頁面上的按鈕跳轉到APP的另一個頁面?

比如說點擊H5頁面上的儲值按鈕,跳轉到儲值介面,點擊立即搶購按鈕,跳轉到商品頁面。

H5與APP端是如何進行資料互動的?

後台該如何寫?

有沒有例子?

請大牛之支招。

上面幾個方法都挺不錯的。但其實只是單純跳轉問題,不用搞得那麼複雜。
從webview中抓取跳轉資訊,然後android端和前段商量好介面,就直接處理就好了。
例如:web中有個跳轉

跳轉設定

點擊後會出發webview中的shouldOverrideUrlLoading函數,Android端:

webView.setWebViewClient(new WebViewClient() {            @Override            public boolean shouldOverrideUrlLoading(WebView view, String url) {                L.i(url); // 擷取到 example://jumpToSettings ,然後接下來就是字串處理了                optionUrl(url); // 判斷如果是跳轉字串,進行跳轉                return true; // 消費,不讓網頁跳轉            }            @Override            public void onPageStarted(WebView view, String url, Bitmap favicon) {//                super.onPageStarted(view, url, favicon);            }        });

JavaScriptCore網上很多例子的,apple文檔也很詳細

Android端,WebView載入H5,H5中js代碼可以這樣:

在當前WebView所在Activity這樣寫:

mWebViewContent.addJavascriptInterface(new JumpAppInterFace(mContext),"toActivity");

其中,JumpAppInterFace 就是你需要注入的跳轉到另一Activity的類,大致長這樣:

public class JumpAppInterFace {    private static final String TAG = "JumpAppInterFace";        private android.content.Context Context;        public JumpAppInterFace(android.content.Context Context) {            this.Context = Context;        }        @JavascriptInterface        public void OpenLinkH5(String url){            if (!TextUtil.isEmpty(url)){                Intent intent=new Intent(Context, AnotherActivity.class);                intent.putExtra("url",url);                Context.startActivity(intent);            }        }}

stackoverflow上看到的,你看行不行.

webview.addJavascriptInterface(new Object() {    @JavascriptInterface    public void openActivity(String activity){        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(activity));        mContext.startActivity(i);    }}, "android");儲值搶購
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.