所以我需要webview中的用户点击链接,在这种情况下,它是一个包含.pdf文件的链接。我有启动PDF阅读器的代码,但它没有获得链接,所以它只是在点击PDF文件时加载PDF阅读器。如何拦截此链接并将其提供给我的PDF阅读器意图?
代码:
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 < 100 && 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;
}
}
答案 0 :(得分:1)
我不知道你的HTML代码的结构,但我会假装你有一个按钮。这里是你的html中的代码:
<button onclick='openPdfBtn.performClick();'>Open pdf</button>
然后在webview中添加一个js界面:
Button openPdfBtn = new Button(this); //this is not gonna be visible
yourWebView.addJavascriptInterface(openPdfBtn, "openPdfBtn"); //here you bind the js with the native button
openPdfBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openPdf();
}
});