Back

android + vue: 让某些页面点击后, 原activity不会跳转.(仅仅给自己看的笔记)

发布时间: 2017-05-22 09:03:00

说明,以下过程 仅仅适用于我们公司的某个项目. 

1.  在main.js 中,(同路由路径), 有这个函数,可以判断是否点击url后需要跳转.

router.beforeEach((to, from, next) => {
 if (from.query.client === 'android'){
    console.info('== by android' )
    console.info('to.full_path: ' + to.fullPath)
    // 如果要打开的页面是: 装修服务详情页, 起始页面是:首页,/home
    console.info("from.full_path: " + from.fullPath)

    if(to.fullPath.includes('zhuang_xiu_fu_wu_show') || 
        to.fullPath.includes('/decorate_live')
        ){
      link = to.query.is_open_new_activity === 'true'
      console.info("== link is: " + link)
    }
}
  next(link)
})

2. 在android Activity中,打开webview时, 要注入 js object:

// this 就是当前的activity
webview.addJavascriptInterface(this, "android");


3. 同时,在上面代码的 this 对应的class中(也就是当前的 Activity) ,增加函数:

    @JavascriptInterface
    public void toZhuangXiuFuWuForJs(String id){
        Intent intent = new Intent(MainActivity.this, ZhuangXiuFuWuActivity.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }

    // 另外,需要在当前的Activity的onCreate 方法中,打开对应的h5页面: 
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //  ... 其他代码。

        url = PageUrls.SHOP_URL;
        Map options = new HashMap();
        Tool.openWebView(webview, url, options);   // 使用这个打开的话,就会看到   ...?client=android 
    }

4. 在对应的vue.js的页面中,增加对于 该js object的调用:

 11           <div style="background-color: white; padding-bottom: 15px;"
 12             @click="click_for_android_zhuang_xiu_chan_pin(product.id)"
 13             >

同时,对应的方法是:
methods : {
  click_for_android_zhuang_xiu_chan_pin(id) { 
    if(this.$route.query.client == 'android') {
        android.toZhuangXiuFuWuForJs(id)
    }
  }
}

5. 在“要被打开的" Activity的onCreate 方法中,增加 webView.load的方法:

        setWebView();
       // 在 setWebView 之后...
        Map options = new HashMap();
        options.put("isOpenNewActivity", true);
        Tool.openWebView(productWebView, url, options);  // 这里就会带上 client=android 等参数 。 



6. 补上tool.java:

public class Tool {

    /**
     * 用来让webview打开 某个url时, 加上辅助的参数, 例如 client=android&is_open=true
     * @param options
     * isOpenNewActivity:   true/false,   Boolean
     */
    public static void openWebView(WebView webView, String url, Map options){
        Boolean isOpenNewActivity = (Boolean)options.get("isOpenNewActivity");
        String finalUrl = url + getConnector(url) + "client=android" + "&is_open_new_activity=" + isOpenNewActivity;

        Log.i("Tool", "== finalUrl: " + finalUrl);
        webView.loadUrl(finalUrl);
    }
    private static String getConnector(String url){
        return url.indexOf("?") > 0 ? "&" : "?";
    }
}

Back