gpt4 book ai didi

java - Android - 从Webview拦截链接

转载 作者:行者123 更新时间:2023-12-01 15:23:27 24 4
gpt4 key购买 nike

所以我需要用户在 webview 中单击链接,在本例中它是包含 .pdf 文件的链接。我有启动 PDF 阅读器的代码,但它没有获取链接,因此它只是在单击 PDF 文件时加载 PDF 阅读器。如何拦截此链接并将其提供给我的 PDF 阅读器 Intent ?

代码:

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;
}
}

最佳答案

我不知道你的html代码的结构,但我会假装你有一个按钮。这是您的 html 中的代码:

<button onclick='openPdfBtn.performClick();'>Open pdf</button> 

然后添加一个js接口(interface)到你的webview中:

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();
}
});

关于java - Android - 从Webview拦截链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10525959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com