如何在浏览器中打开外部WebView链接?

时间:2019-07-14 12:59:49

标签: javascript java android android-studio webview

我想在浏览器中打开外部Webview链接,所以请更新我的代码。实际上,问题是我的Web视图包含一些Google驱动器链接,因此我想在浏览器中打开所有外部链接,并且我在网络上对其进行了过多搜索,但是我找不到正确的解决方案。 我是android studio的新手,所以请帮助我。

package com.iaritoppers;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import android.renderscript.Sampler;
import android.view.KeyEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatRatingBar;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    //initializing WebView
    private WebView mwebView;
    private WebView mWebviewPop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AppRater.app_launched(this);

        mwebView = (WebView) findViewById(R.id.myWebView);
        WebSettings webSettings = mwebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(true);
        mwebView.loadUrl("https://www.iaritoppers.com/");
        mwebView.setWebViewClient(new MyWebviewClient());
        mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent shareintent =new Intent();
                shareintent.setAction(Intent.ACTION_SEND);
                shareintent.putExtra(Intent.EXTRA_TEXT , "abc");
                shareintent.setType("text/plain");
                startActivity(Intent.createChooser(shareintent,"Share Via"));
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = 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_icar) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/icar-e-course-pdf.html");
            return true;
        }
        if (id == R.id.action_share) {
            Intent shareintent =new Intent();
            shareintent.setAction(Intent.ACTION_SEND);
            shareintent.putExtra(Intent.EXTRA_TEXT , "*I recommend you to download this very useful App For Agriculture Students -* ");
            shareintent.setType("text/plain");
            startActivity(Intent.createChooser(shareintent,"Share Via"));
        }

        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_home) {
            Intent i = new Intent(MainActivity.this, MainActivity.class);
            startActivity(i);
        } else if (id == R.id.nav_icar) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/icar-e-course-pdf.html");
        } else if (id == R.id.nav_agronomyicar) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/agronomy-icar-ecourse-pdf-books.html");
        } else if (id == R.id.nav_hortiicar) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/horticulture-icar-ecourse-pdf-books.html");
        } else if (id == R.id.nav_dairyicar) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/dairy-technology-icar-e-course-free-pdf.html");
        } else if (id == R.id.nav_tnau) {
            mwebView.loadUrl("https://www.iaritoppers.com/2019/06/tnau-notes-pdf-download-agri-study-material.html");
        } else if (id == R.id.nav_angrau) {
            mwebView.loadUrl("https://www.iaritoppers.com/2019/06/angrau-notes-download-angrau-study-material.html");
        } else if (id == R.id.nav_syllabus) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/agriculture-exams-syllabus.html");
        } else if (id == R.id.nav_jrfold) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/icar-jrf-old-papers.html");
        } else if (id == R.id.nav_bhuold) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/bhu-old-paper-ug-and-pg.html");
        } else if (id == R.id.nav_iariold) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/iari-old-question-papers-download-for.html");
        } else if (id == R.id.nav_prepgold) {
            mwebView.loadUrl("https://www.iaritoppers.com/2019/07/rajasthan-pre-pg-agriculture-old-question-paper.html");
        } else if (id == R.id.nav_afoold) {
            mwebView.loadUrl("https://www.iaritoppers.com/2019/06/ibps-afo-old-question-papers-pdf-download-for-agriculture-field-officer.html");
        } else if (id == R.id.nav_agribook) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/best-agriculture-books-for-icar-jrf-srf.html");
        } else if (id == R.id.nav_entrance) {
            mwebView.loadUrl("https://www.iaritoppers.com/search/label/agriculture%20entrance%20exams");
        } else if (id == R.id.nav_jobs) {
            mwebView.loadUrl("https://www.iaritoppers.com/search/label/Latest%20agriculture%20jobs");
        } else if (id == R.id.nav_discussion) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/discussion-desk_23.html");
        } else if (id == R.id.nav_top) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/our-top-contributors.html");
        } else if (id == R.id.nav_contact) {
            mwebView.loadUrl("https://www.iaritoppers.com/p/about-us.html");
        } else if (id == R.id.nav_share) {
        Intent shareintent =new Intent();
        shareintent.setAction(Intent.ACTION_SEND);
        shareintent.putExtra(Intent.EXTRA_TEXT , "*I recommend you to download this very useful App For Agriculture Students -* ");
        shareintent.setType("text/plain");
        startActivity(Intent.createChooser(shareintent,"Share Via"));

    }

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    private class MyWebviewClient extends WebViewClient {



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

            super.onPageStarted(view, url, favicon);
            findViewById(R.id.progressBar2).setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            findViewById(R.id.progressBar2).setVisibility(View.GONE);
        }

    }
    //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
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setMessage("Are you sure you want to exit?")
                                .setCancelable(false)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        finish();
                                    }
                                })
                                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });
                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

}

1 个答案:

答案 0 :(得分:1)

我建议使用chrome自定义标签。 这是指向docs

的链接

但是,刚刚在这里粘贴了您需要的信息:

首先,将Chrome自定义标签添加到您的Gradle文件中

dependencies {
    ...
    compile 'com.android.support:customtabs:23.3.0'
}

然后创建chrome自定义标签并显示它:

// Use a CustomTabsIntent.Builder to configure CustomTabsIntent.
// Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent
// and launch the desired Url with CustomTabsIntent.launchUrl()

String url = ¨https://paul.kinlan.me/¨;
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));