如何在Android主屏幕上添加书签(URL)

3

我尝试手动在主屏幕上添加书签,现在需要一些关于如何以编程方式实现它的帮助。谢谢。

3个回答

3
这对我有效 - 在清单中添加权限
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

请使用以下代码:
    public void createGoogleSearchShortcut(Context context) {
    String urlStr = String.format(context.getString(R.string.homescreen_shortcut_search_url), context.getString(R.string.app_id));
    Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlStr));
    // shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.search));
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_search));
    intent.putExtra("duplicate", false);

    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    context.sendBroadcast(intent);

}

请注意:我强烈建议在添加书签之前询问用户。未经用户许可将书签添加到他的主屏幕是“不礼貌”的行为...

0

请查看(文档不太好的)操作INSTALL_SHORTCUT。您需要相应的权限,如链接中所述。


0

谢谢您的回复,但我想说的是在您的主屏幕上放置了一个书签,您可以从那里启动网站。如果您在Android中打开浏览器并转到书签,您实际上可以将该书签的快捷方式放置在主屏幕上。还有更多的想法吗?谢谢。 - dackyD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接