Android phone call from webview in a viewpager

0

I have a web page viewpager. It is very simple and works well. But when I click on the href="tel:971142254"> link, try to access the website and I get an error. I tried this, but it gives me the same error.

public boolean shouldOverrideUrlLoading(WebView view, String 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:")) {
            view.loadUrl(url);
        }
        return true;
    }

I leave the code. Thanks and best regards.

public class Comercio2 extends FragmentActivity{

    ViewPager pager;
    FragmentStatePagerAdapter adapter;

    /* Just some random URLs
    *
    * Each page of our pager will display one URL from this array
    * Swiping, to the right will take you to the next page having
    * the next URL.
    */
    String[] toVisit={
            "http://www.angoher2.com/alimenta1b.html",
            "http://www.angoher2.com/alimenta2b.html",
            "http://www.angoher2.com/alimenta3b.html",            
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comercio2);
        pager=(ViewPager)findViewById(R.id.container2);

        TabLayout container_tab2 = (TabLayout) findViewById(R.id.container_tab2);
        container_tab2.setupWithViewPager(pager, true);

        adapter=new FragmentStatePagerAdapter(getSupportFragmentManager()){

            @Override
            public int getCount() {
                // This makes sure getItem doesn't use a position
                // that is out of bounds of our array of URLs
                return toVisit.length;
            }

            @Override
            public Fragment getItem(int position) {

                    // Here is where all the magic of the adapter happens
                // As you can see, this is really simple.
                return Fragment_Web.newInstance(toVisit[position]);
            }
        };

        //Let the pager know which adapter it is supposed to use
        pager.setAdapter(adapter);
    }
}

This is the fragment.

public class Fragment_Web extends Fragment{

    WebView browser;
    String url;

    @Override
    public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_web, container, false);
        browser=(WebView)view.findViewById(R.id.my_browser);
        browser.setWebViewClient(new WebViewClient());
        browser.loadUrl(url);

        return view;
    }

    // This is the method the pager adapter will use
    // to create a new fragment
    public static Fragment newInstance(String url){

        Fragment_Web f = new Fragment_Web();
        f.url = url;
        return f;
    }
}
    
asked by Andreu 10.01.2018 в 17:29
source

1 answer

0

In your first code snippet that you say did not help you solve your problem, change it to this:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("tel:")) { 
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); 
        startActivity(intent);
        view.reload();
        return true;
    }

    view.loadUrl(url);
    return true;
}

Other possible solutions: link

    
answered by 10.01.2018 в 20:38