简体   繁体   中英

Android Studio webview call link work only once

My activity java is like this. The problem is the html Call link

<a href="tel:555-555-5555">

only works once. I tried many things but i couldn't get it to work. How can i fix that?

public class Wall extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    String url = "http://##########.com/#############/";
    setContentView(R.layout.activity_wall);
    WebView webView = (WebView)findViewById(R.id.webView);
    webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB
    webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() );
    webView.getSettings().setAllowFileAccess( true );
    webView.getSettings().setAppCacheEnabled( true );
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default
    if ( !isNetworkAvailable() ) { // loading offline
        webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
    }
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
                webView.reload();
                return true;
            }
            if (url.startsWith("sms:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
                webView.reload();
                return true;
            }
            webView.loadUrl(url);
            return true;
        }
    });
    webView.setWebChromeClient(new WebChromeClient());
    webView.loadUrl("http://zacseed.com/parttime/");



    }



@Override
public void onBackPressed() {

    WebView webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient());
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }

}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE );
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}}

And i tried to set up a button to copy a number to clipboard with javascript. But that didn't worked as well. In the browser it works fine. But not in the app

Please Check this Code Snippet

 @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         if (url.indexOf("tel:") > -1) {
            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
            return true;
        } 
    }

Try this:

private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mailto:") || url.startsWith("mms:") || url.startsWith("mmsto:") || url.startsWith("market:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            } else {
                view.loadUrl(url);
                return true;
            }
        }
    }

That should normally open the phone app. If you have any more questions just ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM