Webview - 在外部应用程序和浏览器/android 中打开链接 [英] Webview - open links in external apps and browsers / android

查看:48
本文介绍了Webview - 在外部应用程序和浏览器/android 中打开链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是制作 android 应用程序的初学者.我已经用 HTML 制作了一个 Web 应用程序,我希望能够在我在 android studio 中制作的应用程序中使用它.我设法在 android studio 中创建了一个简单的 web 视图,这使得我的 web 应用程序在我的设备上测试时可以正常工作.唯一的问题是 web 视图处理我的 web 应用程序中的所有 URL.Web 应用程序由选项卡组成,当我点击它们时,这些选项卡会将我定向到不同的页面,这正是我想要的.但是我有联系按钮和不同的链接,我想从网络视图中释放"它们.让我们以联系按钮为例.我有一个 Galaxy 笔记,我用它来测试我的应用程序.当我在手机上打开我的应用程序时,我会看到我的 Web 应用程序并且我可以四处浏览.当我单击联系按钮时,Web 视图会处理该链接并显示页面无法加载",而不是在我的手机上打开邮件应用程序.我还有一些带有链接的按钮,我希望能够在手机的外部浏览器中打开这些链接.我希望你理解我的问题,我很抱歉我的英语不好.

I am a beginner in making android applications. I have made a web application in HTML which i want to be able to use in my application that I am making in android studio. I managed to make a simple web view in android studio which makes my web application work fine when i test it on my device. The only problem is that the web view handles all the URL´s inside my web application. The web application consists of tabs that directs me to different pages when i click on them which is what i want. But I have contact-buttons and different links that i want to be "released" from the web view. Lets take the contact button as an example. I have a Galaxy note which I am using to test my apps. When i open my application on my phone i see my Web application and i can navigate around. When i click the contact button, the web view handles the link and gives me a "page could not load" instead of opening the mail application on my phone. I also have buttons with links that I want to be able to open in an external browser on my phone. I hope you understand my problem and I am sorry for my bad english.

这是我的一些网页视图代码.

This is some of my code for the web view.

Mainactivity.java

public class MainActivity extends ActionBarActivity {

WebView browser;

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



    browser = (WebView) findViewById(R.id.wvwMain);

    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setLoadWithOverviewMode(true);
    browser.getSettings().setUseWideViewPort(true);

    browser.setWebViewClient(new ourViewClient());
    try {
        browser.loadUrl("http://WebAppURL");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

OurViewClient.java

public class ourViewClient extends WebViewClient {

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

    return true; 
 }    
}

推荐答案

试试这样实现你的 WebViewClient 就像

Try this way implement your WebViewClient like

 private class VideoWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            try{
                System.out.println("url called:::" + url);
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                }  else if (url.startsWith("http:")
                        || url.startsWith("https:")) {

                     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
                     startActivity(intent);

                }  else if (url.startsWith("mailto:")) {

                    MailTo mt=MailTo.parse(url);

                    send_email(mt.getTo());

                }
                else {
                    return false;
                }
            }catch(Exception e){
                e.printStackTrace();
            }

            return true;
        }

    }

并创建发送邮件功能,如

and create send mail function like

   public void send_email(String email_add) {
    System.out.println("Email address::::" + email_add);

    final Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);
    emailIntent.setType("plain/text");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[] { email_add });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    yourActivity.this.startActivity(
            Intent.createChooser(emailIntent, "Send mail..."));

}

这篇关于Webview - 在外部应用程序和浏览器/android 中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆