当我点击浏览器/邮件中的外部链接时,我的webview-app会打开(这项工作)。
..现在问题: 如果应用关闭,链接将正确打开。 如果应用已打开,则链接无法打开。
我正在考虑使用“shouldOverrideUrlLoading”进行修复,但该函数从未被调用过。
----启动代码----
公共类MainActivity扩展了Activity {
private WebView mWebView;
private Bundle savedInstanceState;
/**
* Dichiariamo l'attributo di classe url in cui
* salviamo l'indirizzo web che aprirà la webview
*/
private String url = "https://asp-center.fgp.biz?app=1&id=";
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (savedInstanceState != null && intent.getData() != null) {
mWebView.restoreState(savedInstanceState);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//carichiamo la webview dentro il layout specificato
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadsImagesAutomatically(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
//permette di gestire i _blank
mWebView.getSettings().setSupportMultipleWindows(false);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult();
String data = result.getExtra();
Log.d("DATA", "" + data);
// view.loadUrl(data);
WebView newWebView = new WebView(view.getContext());
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(view);
resultMsg.sendToTarget();
return true;
}
});
mWebView.requestFocusFromTouch();
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
// prendo l'id del dispositivo che mi aiuta a controllare se è attivo l'auto-accesso (non sono richieste credenziali) per il suddetto dispositivo
String android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
//scrivo del testo di debug
TextView t = (TextView)findViewById(R.id.textView);
t.setText("start");
//imposto la url di default per l'apertura della app
String url = "http://fgp.intranet/ggweb/gol_home_mobile.php?app=1&id_device=" + android_id;
//controllo se l'apertura della app è stata fatta da un link esterno (come le mail di notifica richieste)
if(intent.getData() != null) {
//se è stata aperta esternamente, apro il link passato
url = intent.getData().toString();
t.setText(url);
//url2 = intent.getData().toString();
} else {
}
//carichiamo la url nella webview
mWebView.loadUrl(url);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
TextView t = (TextView)findViewById(R.id.textView);
t.setText("shouldOverrideUrlLoading!");
System.out.println("here");
if (Uri.parse(url).getHost().equals("fgp.intranet")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
/**
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
TextView t = (TextView)findViewById(R.id.textView);
t.setText("bbbbbbbb!");
String url=request.getUrl().toString();
view.loadUrl("https://www.google.com");
return true;
}
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:1)
你需要处理inNewIntent。如果活动已经打开,则创建永远不会再次调用。
@Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
if (intent == null) {
return;
}else{
// Your code to handle intent
}
}