Android中创建快捷方式代码实例

前端技术 2023/09/03 Android

1、添加权限(必须)

复制代码 代码如下:

<uses-permission android:name=\"com.android.launcher.permission.INSTALL_SHORTCUT\" />

2、添加快捷键

复制代码 代码如下:

    public static void setupShortcut(Activity activity)
    {
        Intent shortcutIntent = new Intent(activity, MainActivity.class); //启动首页(launcher Activity)

        Intent intent = new Intent(\"com.android.launcher.action.INSTALL_SHORTCUT\");
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, \"hello\");//快捷键名字可以任意,不过最好为app名称
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        intent.putExtra(\"duplicate\", false);//不允许重复创建

        activity.sendBroadcast(intent);//发送广播创建快捷键
    }

本文地址:https://www.stayed.cn/item/7197

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。