Android 桌面图标长按快捷菜单

为了更快使用APP的某个功能,在手机桌面长按图标呼出快捷菜单
需要注意:在7.0以上的系统才支持该功能

两种方案可以实现该功能,第一种静态文件,不过基本上满足不了日常需求。第二种就是动态加载。

ShortcutManagerCompat方式实现:AndroidX版本

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
        List shortcutInfoList  = new ArrayList<>();
            ShortcutInfoCompat.Builder builder = new ShortcutInfoCompat.Builder(this,"这里设置唯一标识就可以,内容随意");
            builder.setShortLabel("快捷菜单1").setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
                    .setIntent(new Intent(Intent.ACTION_MAIN, null,this,SttActivityGoogle.class));
            shortcutInfoList.add(builder.build());
        ShortcutManagerCompat.addDynamicShortcuts(this,shortcutInfoList);
    }

ShortcutManager方式实现:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(this, "这里设置唯一标识就可以,内容随意")//唯一标识id
                    .setShortLabel("快捷菜单")//短的标签
                    .setLongLabel("快捷菜单123")//长的标签
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))//图标
                    .setIntent(new Intent(this, NiuHaoActivity.class))//跳转的目标,这里我设置的是当前
                    .build();
           activity.getSystemService(ShortcutManager.class).setDynamicShortcuts("这里设置list,转换一下即可");
        }

你可能感兴趣的:(android)