Android Studio如何打开WebView链接到新活动

时间:2019-05-27 14:28:26

标签: java android android-activity webview

我正在编写这段代码以打开指向Activity的链接。我在哪里出错了?

我的想法是在Webview上打开我指定的URL时启动活动。

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class anasayfa extends AppCompatActivity {
    private WebView webView;
    private CustomWebViewClient webViewClient;
    private String Url = "https://mobile.twitter.com";
     String dataUrl;
     String testUrl = "https://mobile.twitter.com/signup";
    ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anasayfa);
        mProgressDialog = new ProgressDialog(this);//
        mProgressDialog.setMessage("Yükleniyor...");

        webViewClient = new CustomWebViewClient();

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setBuiltInZoomControls(true); 
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(webViewClient); 
        webView.loadUrl(Url);


    }

    private class CustomWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) { 
            super.onPageStarted(view, url, favicon);

            if(!mProgressDialog.isShowing())
            {
                mProgressDialog.show();
            }

        }

        @Override
        public void onPageFinished(WebView view, String url) {//sayfamız yüklendiğinde çalışıyor.
            super.onPageFinished(view, url);
            TextView myTextView = (TextView)findViewById(R.id.textView2);
            myTextView.setText(url);


            dataUrl = url;

            if (testUrl==dataUrl)
            {

                Intent intent = new Intent(anasayfa.this, MainActivity.class);
                startActivity(intent);
            }


            if(mProgressDialog.isShowing()){
                mProgressDialog.dismiss();



            }



        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {


            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {





            /*
             *  /** Generic error
            public static final int ERROR_UNKNOWN = -1;

            /** Server or proxy hostname lookup failed
            public static final int ERROR_HOST_LOOKUP = -2;

            /** Unsupported authentication scheme (not basic or digest)
            public static final int ERROR_UNSUPPORTED_AUTH_SCHEME = -3;

            /** User authentication failed on server
            public static final int ERROR_AUTHENTICATION = -4;

            /** User authentication failed on proxy
            public static final int ERROR_PROXY_AUTHENTICATION = -5;

            /** Failed to connect to the server
            public static final int ERROR_CONNECT = -6;

            /** Failed to read or write to the server
            public static final int ERROR_IO = -7;

            /** Connection timed out
            public static final int ERROR_TIMEOUT = -8;

            /** Too many redirects
            public static final int ERROR_REDIRECT_LOOP = -9;

            /** Unsupported URI scheme
            public static final int ERROR_UNSUPPORTED_SCHEME = -10;

            /** Failed to perform SSL handshake
            public static final int ERROR_FAILED_SSL_HANDSHAKE = -11;

            /** Malformed URL
            public static final int ERROR_BAD_URL = -12;

            /** Generic file error
            public static final int ERROR_FILE = -13;

            /** File not found
            public static final int ERROR_FILE_NOT_FOUND = -14;

            /** Too many requests during this load
            public static final int ERROR_TOO_MANY_REQUESTS = -15;
            */

        }
    }
    public void onBackPressed() 
    {
        if(webView.canGoBack()){
            webView.goBack();
        }else{
            super.onBackPressed();
        }
    }



}

我正在编写这段代码以打开指向Activity的链接。我在哪里出错了?

我的想法是在Webview上打开我指定的URL时启动活动。

0 个答案:

没有答案