Android 桌面二級菜單

Android應(yīng)用桌面長按app圖標彈出二級菜單,先看效果圖

TIM截圖20171226154331.png

簡單直接上代碼,注意在小米MIUI系統(tǒng)上面不兼容,原因是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("http://www.itdecent.cn/")))
                    .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("http://www.itdecent.cn/")))
                    .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("http://www.itdecent.cn/")))
                    .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("http://www.itdecent.cn/")))
                    .build()

            shortcutManager.dynamicShortcuts = arrayListOf(shortcut1, shortcut2, shortcut3, shortcut4)
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容