我目前正在開發一個webView
的Android應用程序。在某些urls
我需要使用shouldOverrideUrlLoading
方法,這是非常簡單的:如何處理intent://在webView URL上?
if(url.startsWith("something")){
//do something
return true;
}
我的問題:
一些URLS
,在谷歌搜索的例子,有以下URL
方案:
intent://en.m.wikipedia.org/wiki/Test_cricket#Intent;scheme=http;package=org.wikipedia;S.browser_fallback_url=https%3A%2F%2Fwww.google.pt%2Fsearchurl%2Fr.html%23app%3Dorg.wikipedia%26pingbase%3Dhttps%3A%2F%2Fwww.google.pt%2F%26url%3Dhttps%3A%2F%2Fen.m.wikipedia.org%2Fwiki%2FTest_cricket;S.android.intent.extra.REFERRER_NAME=https%3A%2F%2Fwww.google.pt;launchFlags=0x8080000;end
我試過使用:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
但我得到了一個錯誤:
10-15 15:18:15.546: W/System.err(29086): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=intent://en.m.wikipedia.org/wiki/Test_cricket }
我應該如何處理這種URL
方案?
UPDATE:
Intent myIntent = new Intent().parseUri(url, Intent.URI_INTENT_SCHEME);
startActivity(myIntent);
:
繼@CommonsWare意見,我已經使用上Intent
parseUri()
創建從URL
一個Intent object
,然後嘗試startActivity()
,類似嘗試但我仍然得到:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://en.m.wikipedia.org/wiki/Test_cricket flg=0x8080000 pkg=org.wikipedia (has extras) }
我可能得到的指示錯誤或者我根本無法構建@CommonsWare指定的intent
。有關如何構建Intent
的任何提示?
要麼趕上'ActivityNotFoundException'或使用'PackageManager'和'resolveActivity()'調用之前檢測到這種情況'startActivity()'。在這種情況下,'Uri'是一個應用程序(維基百科),如果該應用程序沒有安裝,「Intent」將無法解析。您將需要向用戶提供某種錯誤消息,指出這一事實。 – CommonsWare
@CommonsWare如果點擊一個帶有'intent:// ...'的鏈接,一些瀏覽器(Firefox,Chrome,Opera)成功打開該應用程序,是否有辦法將uri轉發到默認瀏覽器並讓它處理它呢? –
默認瀏覽器沒有處理它 - 維基百科應用程序應該這樣做。儘管現在我想到了,但「ACTION_VIEW」可能不是正確的答案。使用'Intent'上的'parseUri()'從URL創建一個'Intent'對象,然後在'Intent'上嘗試'startActivity()'。 – CommonsWare