0

是否可以向 shouldOverrideUrlLoading 添加一个条件,在将其他链接定向到移动浏览器的同时将某些链接保留在应用程序中?

我最初的想法是这样的:

if (url.contains("myurl.com")){
    //open the url in the app
}
else{
    //open the url in the mobile browser
}

以我有限的理解,这是我能想到的最好的主意。我对其他想法持开放态度,任何语法帮助将不胜感激。

4

1 回答 1

1

your initital thoughts are correct.
in fact, I beleive shouldOverrideUrlLoading() is a part of the API for doing exactly that.

open the url in the app =

super.shouldOverrideUrlLoading();
mYourWebViewInstance.loadUrl(url);
return true;

open the url in the mobile browser =

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url ));
startActivity(browserIntent);
return true;

this code will launch directly app that can handle this data type, which be in this case any browser app installed on your device, or will give you intent chooser to select one of your browsers to show the url if you have more then one web browsers installed and none of them set to be default (always open with...)

returning true according to the documentation means you handled the url yourself, and the event is consumed.

there is also an issue of inconsistency across android platforms and certain firmwares - from my experience, I've seen that in some devices not overriding this callback will returns false by default (will trigger intent chooser for external web browser), and some devices returns true by default.

that's why you always have to override this callback, for making sure you know exactly how your app behaves on all devices.

so, is it answering your question or not?

于 2013-08-22T16:18:00.830 回答