我正在将网站转换为应用程序,并且链接未在应用内打开。 Android Studio

时间:2017-06-28 20:54:24

标签: java android android-webview

我正在将网站转换为应用,并且链接未在应用内打开。我做了很多改变但没有发生任何事情。那么请你帮帮我吧。怎么解决 。我希望在应用程序中打开所有链接,我的网站中的My Youtube视频也不会全屏打开。 提前致谢

Main Activity Code --

    package com.mywebsite.fun;

    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.view.KeyEvent;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    import java.net.URI;

    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {

        //initializing WebView
        private WebView mwebView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);

            //WebView
            mwebView = (WebView) findViewById(R.id.myWebView);
            WebSettings webSettings = mwebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            //improve webView performance
            mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            mwebView.getSettings().setAppCacheEnabled(true);
            mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            webSettings.setDomStorageEnabled(true);
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
            webSettings.setUseWideViewPort(true);
            webSettings.setSavePassword(true);
            webSettings.setSaveFormData(true);
            webSettings.setEnableSmoothTransition(true);


            mwebView.loadUrl("https://laughonlyblog.wordpress.com/about/");
            //force links open in webview only
            mwebView.setWebViewClient(new MyWebviewClient());

        }

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
            int id = item.getItemId();

            if (id == R.id.nav_camera) {
                // Handle the camera action
                mwebView.loadUrl("http://www.newsweek.com/us");
            } else if (id == R.id.nav_slideshow) {
                mwebView.loadUrl("http://www.newsweek.com/world");
            } else if (id == R.id.nav_manage) {
                mwebView.loadUrl("http://www.newsweek.com/tech-science");
            } else if (id == R.id.nav_gallery) {
                mwebView.loadUrl("http://www.newsweek.com/sports");
            } else if (id == R.id.nav_share) {
                mwebView.loadUrl("http://www.newsweek.com/about-newsweek");
            } else if (id == R.id.nav_send) {
                mwebView.loadUrl("http://www.newsweek.com/contact");
            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }

        private class MyWebviewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().equals("www.laughonlyblog.wordpress.com/about/")) {
                    //open url contents in webview
                    return false;
                } else {
                    //here open external links in external browser or app
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }

            }
            //ProgressDialogue
            ProgressDialog pd = null;


           @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                pd=new ProgressDialog(MainActivity.this);
                pd.setTitle("Please Wait..");
                pd.setMessage("Website is Loading..");
                pd.show();
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                pd.dismiss();
                super.onPageFinished(view, url);
            }
        }
        //goto previous page when pressing back button

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        if (mwebView.canGoBack()) {
                            mwebView.goBack();
                        } else {
                            finish();
                        }
                        return true;
                }
            }
            return super.onKeyDown(keyCode, event);
        }
    }



    Manifest Code---

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.mywebsite.fun">

        <uses-permission android:name="android.permission.INTERNET" />
        android:hardwareAccelerated="true"


        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <intent-filter>
                   <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>


            </activity>
        </application>

    </manifest>

1 个答案:

答案 0 :(得分:0)

尝试在MyWebviewClient内部类的方法'shouldOverrideUrlLoading'中注释这些行:

 @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
           /* if (Uri.parse(url).getHost().equals("www.laughonlyblog.wordpress.com/about/")) {
                //open url contents in webview
                return false;
            } else {
                //here open external links in external browser or app
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);

            } */
return true;
        }