使用数据 URI 方案而不是 URL 打开浏览器 [英] Open browser with Data URI Scheme instead of a URL

查看:36
本文介绍了使用数据 URI 方案而不是 URL 打开浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我用这个方法破坏了我的脑细胞,但没有解决方案......

Well, I was breaking the hell of my brain cells with this and no solution came up...

通常,在 Android 中,要在指定的网站中打开 Web 浏览器,我们这样做:

Usually, in Android, to open the Web Browser in a specified Website, we do this:

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

所以,我得到了一个像这样的数据 URI 方案(不知道是不是这样写的,我不是这类东西的专家):

So, I got a Data URI Scheme (dunno if it is written this way, i'm not an expert on this kind of stuff) like this:

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

如果我将其复制并粘贴到网络浏览器中,它会按照我想要的方式进行处理.

If I copy and paste this in a web browser, it will handle it the way I want it.

但我如何在 Android 中以编程方式执行此操作?

But how can I do it programatically in Android?

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

dataHTMLBase64 存储了我之前提到的 Data URI Scheme.

dataHTMLBase64 stores the Data URI Scheme I mentioned before.

上面的代码不起作用.它甚至不会启动 chrome.

The code above won't work. It won't even launch chrome.

我能做什么?

PS:我英语不好.如果我没有正确表达自己,请警告我...

PS: I'm not good with English. Please warn me if I didn't express myself the right way...

推荐答案

实际上,在 Android 浏览器中启动 Data URI 似乎很容易.

Actually, it appears that one can launch a Data URI in an Android browser quite easily.

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 我查看了 AndroidManifest.xml谷歌浏览器 .apk 文件.
(apktool 很容易安装,然后命令很简单apktool d example.apk)


Using apktool I reviewed the AndroidManifest.xml of the Google Chrome .apk file.
(apktool is quite easy to install, and then the command is simply apktool d example.apk)

我找到了相关的意图过滤器(如下所列),因此有许多可能的方式来启动浏览器.当然其他浏览器可能有不同的意图过滤器,但似乎APP_BROWSER是一个不错的选择.

I found the relevant intent filters (listed below) so there are many possible ways to launch the browser. Of course other browsers may have different intent filters, but it seems that APP_BROWSER is a good choice.

<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>

这篇关于使用数据 URI 方案而不是 URL 打开浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆