android 中与 vuejs的集成: 跳转到普通的url ( 注意没有交互, 就是普通的跳转)

访问量: 258

1. activity中, 对url 进行拦截: 

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.d(TAG, "onPageFinished, url: " + url);

            Matcher matcher = Pattern.compile("login").matcher(url);
            if(matcher.find()){
                Log.d(TAG, "to login page");
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
       
       }

2. 创建对应的目标Activity  ( 我整个粘过来的,其实没有太大作用, 有一个webview, 和对应的 xml就可以了)

package topgroup.com.topgroupandroid;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RadioButton;
import android.widget.Toast;

public class LoginActivity extends BaseActivity  implements View.OnClickListener{

    private WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.login_activity);
        initView();
        setWebView();
        Log.d("== in login activity", " loading login");

        showLoadingDialog();
        webview.loadUrl(appendParameters(PageUrls.LOGIN));
    }

    private void initView() {
        webview = (WebView) findViewById(R.id.login_web_view);
    }

    private void setWebView() {
        radioButton3.setChecked(true);

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        WebSettings settings = webview.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);    //排版适应屏幕
        settings.setLoadWithOverviewMode(true);                             // setUseWideViewPort方法设置webview推荐使用的窗口。setL
        settings.setUseWideViewPort(true);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowFileAccess(true);
        settings.setDefaultTextEncodingName("UTF-8");
        webview.setBackgroundColor(0);
        webview.setVisibility(View.VISIBLE);
        webview.setWebViewClient(new gameWebViewClient());
    }

    @Override
    public void onClick(View view){
        switch(view.getId()){
            case R.id.button_home:
                Intent intent_home = new Intent(this, MainActivity.class );
                startActivity(intent_home);
                finish();
                break;
        }
    }




    public class gameWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d(TAG, "onPageFinished, 触发的url是====" + url);
            view.loadUrl(appendParameters(url));
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Log.d(TAG, "onPageStarted, 触发的url是====" + url);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            Log.d(TAG, "shouldInterceptRequest, 触发的url是====" + url);
            return super.shouldInterceptRequest(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

//            getUserId();
//
//            Log.d(TAG, "onPageFinished, 触发的url是====" + url);
//            Pattern pattern = Pattern.compile("product");
//            Matcher matcher = pattern.matcher(url);
//            if (matcher.find()) {
//                Log.d(TAG, "拦截到了,跳转产品详情===");
//                //跳转到产品activity
//                toProduct(url);
//            }



            Log.d(TAG, "取消小菊花开始...");
            // 取消小菊花 放在这里才可以.   针对myorder  打开的时候小菊花不会消失。
            try {
                dismissLoadingDialog();
            }catch(Exception e){
                try{
                    dismissLoadingDialog();
                }catch(Exception e2) {
                    Log.e(TAG, "== 取消小菊花出错了: " + e.toString());
                }
            }
        }
    }


    boolean doubleBackToExitPressedOnce = false;

    @Override
    public void onBackPressed() {
//        if (doubleBackToExitPressedOnce) {
//            super.onBackPressed();
//            return;
//        }
//
//        this.doubleBackToExitPressedOnce = true;
//        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
//
//        new Handler().postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                doubleBackToExitPressedOnce=false;
//            }
//        }, 2000);
    }
}

3. 对应的 XML;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pure_white"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="topgroup.com.topgroupandroid.LoginActivity"
android:weightSum="1">
<!-- 这个是最最重要的, 有个web view -->
<WebView
android:id="@+id/login_web_view"
android:layout_width="fill_parent"
android:layout_height="239dp"
android:layout_weight="1.01">
</WebView> </LinearLayout>

最后记得在  android manifest.xml 中声明上面的activity (login activity)就好了.

订阅/RSS Feed

Subscribe

分类/category