在Android上启动Chrome作为网络应用启动

卢卡斯

我有一个非常具体的问题。我已经在Android平板电脑上实现了一个Web App,该Web App将在展览中使用(Outform iDisplay)。因此,Web App必须在启动后直接启动。引导后的东西没问题(广播“ android.permission.RECEIVE_BOOT_COMPLETED”),但是我在启动Chrome作为Web应用程序时遇到了问题。为了获得意图,我已经阅读了带有以下代码段的启动器收藏夹中的图标:

    //Kitkat, therefore launcher3
    url = "content://com.android.launcher3.settings/favorites?Notify=true";

    ContentResolver resolver = getContentResolver();
    Cursor cursor = resolver.query(Uri.parse(url), null, null, null, null);

    if (cursor != null && cursor.moveToFirst())
    {
        do
        {
            String ent1 = cursor.getString(0);
            String ent2 = cursor.getString(1);
            String ent3 = cursor.getString(2); //there is the Intent string
            String ent4 = cursor.getString(3);
            System.out.println("Test");
            String ent5 = cursor.getString(4);
            String ent6 = cursor.getString(5);
            String ent7 = cursor.getString(6);
            String ent8 = cursor.getString(7);
            String ent9 = cursor.getString(8);
            String ent10 = cursor.getString(9);
            String ent11 = cursor.getString(10);
            String ent12 = cursor.getString(11);
            String ent14 = cursor.getString(13);
            String ent15 = cursor.getString(14);
            String ent17 = cursor.getString(16);
            String ent18 = cursor.getString(17);
            String ent19 = cursor.getString(18);
            String ent20 = cursor.getString(19);
            if(ent2.equals("History Book")) //Get the right intent
            {
                runAction = ent3;
            }
            System.out.println(ent3);
        } while (cursor.moveToNext());
    }

Intent字符串包含以下内容:

#Intent;action=com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP;package=com.android.chrome;S.org.chromium.chrome.browser.webapp_title=History%20Book;S.org.chromium.chrome.browser.webapp_id=86e362e4-a25d-4142-8a32-c02ffcb176a9;i.org.chromium.content_public.common.orientation=6;S.org.chromium.chrome.browser.webapp_icon=;S.org.chromium.chrome.browser.webapp_mac=3ZaXFbyWnJQaqFFOuUj3OssNz7DrBaaiWfzO2Dd7VIU%3D%0A;S.org.chromium.chrome.browser.webapp_url=http%3A%2F%2F192.168.5.148%2Fstyria%2Fhistorybook%2Findex.html;end

这看起来相当不错,但是我如何在一个小应用程序中启动像这样的Intent呢?

最后,请注意:我已经尝试将其打包到一个webview中,但是由于libc错误,该webview一直死掉,所以这对我来说是没有选择的。

卢卡斯

终于我使这件事起作用了。我的方法是正确的,但是最后的一英里就对Chrome.apk进行了反向工程。我在onCreate中使用以下代码创建了一个虚拟活动:

在主屏幕上搜索正确的条目,以AOSP启动器为例:

    //Search for the History Book Shortcut on the Homescreen
    String url = "";
    String runAction="";

    final String AUTHORITY = "com.android.launcher3.settings";
    final Uri CONTENT_URI = Uri.parse("content://" +
    AUTHORITY + "/favorites?notify=true");

    final ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(CONTENT_URI,null,null,null,null);

    cursor.moveToFirst();
    do {
        String id = cursor.getString(cursor.getColumnIndex("_id"));
        String title = cursor.getString(cursor.getColumnIndex("title"));
        String intent = cursor.getString(cursor.getColumnIndex("intent"));
        if(title.equals(getResources().getString(R.string.homescreen_link)))
        {
            runAction = intent;
        }

    } while (cursor.moveToNext());

在这一点上,我希望将其作为字符串。因此,解析字符串并创建一个新的意图:

    Intent intent = new Intent();
    intent.setAction("com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP");
    intent.setPackage("com.android.chrome");
    intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.webapps.WebappManager");

    HashMap<String, String> intentVals = getIntentParams(runAction);

    intent.putExtra("org.chromium.chrome.browser.webapp_title",intentVals.get("S.org.chromium.chrome.browser.webapp_title"));
    intent.putExtra("org.chromium.chrome.browser.webapp_icon",intentVals.get("S.org.chromium.chrome.browser.webapp_icon"));
    intent.putExtra("org.chromium.chrome.browser.webapp_id",intentVals.get("S.org.chromium.chrome.browser.webapp_id"));
    intent.putExtra("org.chromium.chrome.browser.webapp_url",intentVals.get("S.org.chromium.chrome.browser.webapp_url"));
    intent.putExtra("org.chromium.chrome.browser.webapp_mac",intentVals.get("S.org.chromium.chrome.browser.webapp_mac"));
    int orientation = 6;
    try
    {
        orientation = Integer.parseInt(intentVals.get("i.org.chromium.content_public.common.orientation"));
    }
    catch(NumberFormatException _nex)
    {
        Log.e(TAG, "Wrong format, using default (6)");
    }
    intent.putExtra("org.chromium.content_public.common.orientation", orientation);

    try
    {
        byte[] abyte0 = Base64.decode(
                intentVals.get("S.org.chromium.chrome.browser.webapp_mac"),
                0);
        System.out.println(new String(abyte0));
    }
    catch (IllegalArgumentException _iae)
    {
        Log.e(TAG,
                "Wrong webapp_mac: "
                        + intentVals
                                .get("S.org.chromium.chrome.browser.webapp_mac"));
    }

    startActivity(intent);
    finish();

这个函数从意图字符串中解析出意图参数:

private HashMap<String, String> getIntentParams(String _runAction)
{       
    HashMap<String, String> retMap = new HashMap<String, String>();

    String[] pairs = _runAction.split(";");
    for (int i = 0; i < pairs.length; i++)
    {
         String[] keyval = pairs[i].split("=");
         if(keyval.length==2)
         {
            String key = keyval[0];
            String value = "";
            try
            {
                value = java.net.URLDecoder.decode(keyval[1], "UTF-8");
            }
            catch (UnsupportedEncodingException _uee)
            {
                Log.e(TAG, "Unsupported Encoding: " + _uee.getMessage());
            }
            retMap.put(key, value);
         }
    }
    return retMap;
}

以及res / values中的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">WebAppStarter</string>
    <string name="homescreen_link">History Book</string>
</resources>

而已。您可以配置Homescreen链接名称以在strings.xml中进行搜索。应用找到字符串后,便会解析该意图字符串并创建新意图以将Chrome作为全屏活动网络应用启动。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章