2016-02-27 45 views
0

好打開瀏覽器,我打破我的腦細胞與本和無解的地獄來到了...使用數據URI方案,而不是一個URL

通常情況下,在Android中,打開一個Web瀏覽器指定的網站,我們這樣做:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
startActivity(browserIntent); 

所以,我得到了一個數據URI方案(說不上來,如果它是這樣寫的,我不是這種東西的專家)是這樣的:

data:text/html;charset=utf8;base64,<base64 html code> 

如果我將其複製並粘貼到網頁瀏覽器,它會以我想要的方式處理它。

但是,我怎樣才能以編程方式在Android上執行?

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(dataHTMLBase64)); 
startActivity(browserIntent); 

dataHTMLBase64存儲我前面提到的數據URI方案。

上面的代碼不起作用。它甚至不會啓動Chrome。

我該怎麼辦? PS:我對英文不太好。請提醒我,如果我沒有表達自己的正確方法...

+0

無論如何,如果瀏覽器通常支持'Uri'方案,對於傳入的'intents',我會感到驚訝。當他們遇到來自他們加載的網頁的URL時,他們會在內部處理它。 – CommonsWare

回答

0

如果你從什麼地方得到這些數據的URI,你可以做兩件事情:

  1. 解析數據內容了出來,並用它在某些WebView上並調用loadData(...)來顯示內容
  2. 將URI的數據內容保存到某個文件中,使用FileProvider使該文件在您的應用程序外部可訪問,並使用返回的URI來啓動瀏覽器/查看意圖
1

實際上,人們似乎很容易在Android瀏覽器中啓動數據URI。

String url = "data:text/html;charset=utf8,<b>Hee-haw!</b>"; 

startActivity(Intent.makeMainSelectorActivity(
     Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER) 
     .setData(Uri.parse(url.toString()))); 

使用apktool我回顧了谷歌瀏覽器.apk文件的AndroidManifest.xml
(apktool是安裝很容易,然後在命令僅僅是apktool d example.apk

我找到了相關的意圖過濾器(如下所示),所以有啓動瀏覽器多種可能的方式。當然,其他瀏覽器可能有不同的意圖過濾器,但似乎APP_BROWSER是一個不錯的選擇。

<activity-alias android:exported="true" android:name="com.google.android.apps.chrome.Main" android:targetActivity="org.chromium.chrome.browser.document.ChromeLauncherActivity"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <category android:name="android.intent.category.LAUNCHER"/> 
     <category android:name="android.intent.category.BROWSABLE"/> 
     <category android:name="android.intent.category.APP_BROWSER"/> 
     <category android:name="android.intent.category.NOTIFICATION_PREFERENCES"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <category android:name="android.intent.category.BROWSABLE"/> 
     <data android:scheme="googlechrome"/> 
     <data android:scheme="http"/> 
     <data android:scheme="https"/> 
     <data android:scheme="about"/> 
     <data android:scheme="javascript"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <category android:name="android.intent.category.BROWSABLE"/> 
     <data android:scheme="googlechrome"/> 
     <data android:scheme="http"/> 
     <data android:scheme="https"/> 
     <data android:scheme="about"/> 
     <data android:scheme="content"/> 
     <data android:scheme="javascript"/> 
     <data android:mimeType="text/html"/> 
     <data android:mimeType="text/plain"/> 
     <data android:mimeType="application/xhtml+xml"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.VIEW"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <data android:mimeType="multipart/related" android:scheme="file"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.MEDIA_SEARCH"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.speech.action.VOICE_SEARCH_RESULTS"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.nfc.action.NDEF_DISCOVERED"/> 
     <category android:name="android.intent.category.DEFAULT"/> 
     <data android:scheme="http"/> 
     <data android:scheme="https"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH"/> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="com.sec.android.airview.HOVER"/> 
    </intent-filter> 
    <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> 
</activity-alias> 
相關問題