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

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

问题描述

我是制作android应用程序的初学者.我已经制作了一个HTML HTML Web应用程序,希望能够在我在android studio中制作的应用程序中使用.我设法在android studio中制作了一个简单的Web视图,这使我的Web应用程序在设备上进行测试时可以正常工作.唯一的问题是,Web视图可以处理我的Web应用程序中的所有URL. Web应用程序由选项卡组成,当我单击它们时,这些选项卡将我定向到不同的页面.但是我有联系按钮和不同的链接,希望从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.

这是我的一些Web视图代码.

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天全站免登陆