Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Intercept Link From Webview
    primarykey
    data
    text
    <p>So I need the user clicked link in webview, in this case it is a link containing a .pdf file. I have code that launches the PDF reader but it doesn't get the link so it just loads the PDF reader on click of a PDF file. How do I intercept this link and feed it to my PDF reader intent?</p> <p>Code:</p> <pre><code>public class atcFaa extends Activity { WebView webview; private String url; ProgressBar pd = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.atccti); pd = (ProgressBar) findViewById(R.id.web_view_progress_bar); webview = (WebView) findViewById(R.id.ctiWebView); webview.getSettings().setJavaScriptEnabled(true); Button openPdfBtn = new Button(this); webview.addJavascriptInterface(openPdfBtn, "openPdfBtn"); openPdfBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { openPdf(); } }); webview.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if (progress &lt; 100 &amp;&amp; pd.getVisibility() == ProgressBar.GONE) { pd.setVisibility(ProgressBar.VISIBLE); } pd.setProgress(progress); if (progress == 100) { pd.setVisibility(ProgressBar.GONE); } } }); webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { webview.getSettings().setJavaScriptEnabled(true); // do your handling codes here, which url is the requested url // probably you need to open that url rather than redirect: if (url.startsWith("tel:")) { startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); } else if (url.startsWith("mailto:")) { url = url.replaceFirst("mailto:", ""); url = url.trim(); Intent i = new Intent(Intent.ACTION_SEND); i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[] { url }); startActivity(i); } else if (url.startsWith("geo:")) { try { } catch (Exception e) { System.out.println(e); } } else if (url.endsWith("pdf")) { try { } catch (ActivityNotFoundException e) { Toast.makeText(atcFaa.this, "No PDF Viewer Installed", Toast.LENGTH_LONG).show(); } } else { view.loadUrl(url); } return true; // then it is not handled by default action } }); webview.loadUrl("http://www.faa.gov/air_traffic/publications/"); } protected void openPdf() { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.parse(url); intent.setDataAndType(uri, "application/pdf"); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu2, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.atcAbout2: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG) .show(); break; case R.id.atcContact2: emailme(); break; } return true; } private void emailme() { // TODO Auto-generated method stub String domsEmail = "MYEMAIL@EXAMPLE.com"; String message = "Insert Message Here"; String myemail[] = { domsEmail }; Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, myemail); emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "ATC Assistant"); emailIntent.setType("plain/text"); emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); startActivity(emailIntent); } /* * @Override public void onBackPressed() { if (webview.canGoBack()) * webview.goBack(); else super.onBackPressed(); } */ public void setUrl(String url) { this.url = url; } public String getUrl() { return url; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload