繁体   English   中英

如何以编程方式在 Android 中创建另一个应用程序的快捷方式?

[英]How to create a shortcut of another app in Android programmatically?

我有一些安装 Android 应用程序。 有什么方法可以在主屏幕上创建具有其他名称和图标的任何应用程序的快捷方式吗? 我使用下面的代码但不工作

public void createShortCut(Context context,String name,String packageName, int icon) {
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    shortcutintent.putExtra("duplicate", false);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    Parcelable icon1 = Intent.ShortcutIconResource.fromContext(context, icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon1);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(packageName));
    context.sendBroadcast(shortcutintent);

}

我也用这个,但没有用。

 public void createAppShortCut(Context context, String appName,String packageName, int icon) {

    if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
        ShortcutInfoCompat shortcutInfoCompat = new ShortcutInfoCompat.Builder(context,"#2")
                .setIntent(new Intent(Intent.EXTRA_SHORTCUT_INTENT, Uri.parse(packageName)))
                .setIntent(new Intent("com.android.launcher.action.INSTALL_SHORTCUT"))
                .setShortLabel(appName)
                .setIcon(IconCompat.createWithResource(context, icon))
                .build();
        ShortcutManagerCompat.requestPinShortcut(context, shortcutInfoCompat, null);
        Toast.makeText(context, "ShortCut Created", Toast.LENGTH_SHORT).show();
    } else {
        Log.e(TAG, "launcher does not support short cut icon: ");
        Toast.makeText(context,"launcher does not support short cut icon",Toast.LENGTH_LONG).show();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM