2016-02-27 43 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>