Android将图标添加到主屏幕(启动器)

时间:2017-03-27 16:53:24

标签: android android-browser android-launcher

我正在编写一个浏览器,并希望实现一个按钮"将网站添加到主屏幕"因此,站点图标(apple-touch-icon)和标题将由启动器获取并显示。

我找到了代码 -

countdown

但是,不幸的是,onCreate的意图没有任何额外的数据:

        Intent shortcutIntent = new Intent();
        shortcutIntent.setClassName(getPackageName(), "Browser");
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.addCategory(Intent.ACTION_PICK_ACTIVITY);
        shortcutIntent.putExtra("EXTRA_URL", webView.getUrl());

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, webView.getTitle());
        intent.putExtra(Intent.EXTRA_ORIGINATING_URI, webView.getUrl());

        //TODO change to apple-touch icon
        Bitmap btm = webView.getFaviconImage();
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, btm);

        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(intent);

我应该如何将额外的参数(URL)传递给启动器,以便我能够进一步阅读它?

2 个答案:

答案 0 :(得分:0)

试试这个

intent.putExtra(EXTRA_URL, Uri.parse(webView.getUrl()));

答案 1 :(得分:0)

所以我设法使用下一个代码解决了这个任务:

将快捷方式发送到启动器:

        Intent shortcutIntent = new Intent(getApplicationContext(), BrowserActivity.class);
        shortcutIntent.putExtra(BrowserUnit.EXTRA_URL, webView.getUrl());

        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, webView.getTitle());

        //TODO change to apple-touch icon
        Bitmap btm = webView.getFaviconImage();
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, btm);

        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        sendBroadcast(intent);

撤回正确的网址:

       //in onCreate
       if (getIntent() != null){
        //if we are launching from launcher shortcut
        Bundle bundel = getIntent().getExtras();
        if (bundel != null) {
            String url = bundel.getString(BrowserUnit.EXTRA_URL);
            onSearchPressed(url);
        }
    }
相关问题