Android 桌面二级菜单

Android应用桌面长按app图标弹出二级菜单,先看效果图

TIM截图20171226154331.png

简单直接上代码,注意在小米MIUI系统上面不兼容,原因是MIUI禁用了。
ps:代码用Kotlin编写

class CustomShortcutManager {

    companion object {
        val TAG = "CustomShortcutManager"

        @RequiresApi(api = Build.VERSION_CODES.N_MR1)
        fun initDynamicShortcuts(context: Context){
            val shortcutManager = context.getSystemService(Context.SHORTCUT_SERVICE) as ShortcutManager

            val shortcut1 = ShortcutInfo.Builder(context, "shortcut1")
                    .setShortLabel("我的名片")
                    .setLongLabel("我的名片")
                    .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/")))
                    .build()

            val shortcut2 = ShortcutInfo.Builder(context, "shortcut2")
                    .setShortLabel("扫一扫")
                    .setLongLabel("扫一扫")
                    .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/")))
                    .build()

            val shortcut3 = ShortcutInfo.Builder(context, "shortcut3")
                    .setShortLabel("付款码")
                    .setLongLabel("付款码")
                    .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/")))
                    .build()

            val shortcut4 = ShortcutInfo.Builder(context, "shortcut4")
                    .setShortLabel("收钱码")
                    .setLongLabel("收钱码")
                    .setIcon(Icon.createWithResource(context, R.mipmap.ic_launcher_round))
                    .setIntent(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/")))
                    .build()

            shortcutManager.dynamicShortcuts = arrayListOf(shortcut1, shortcut2, shortcut3, shortcut4)
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。