Android开发经验谈Android开发Android 开发技术分享

简单实现3d touch

2018-05-31  本文已影响42人  浪漫晨风

在应用图标上显示的快捷方式,该快捷方式可以点击进入Activity,长按拖动创建一个在Launcher上的图标。有点类似于苹果的3d touch,现在市场上已经是有很多应用增加了这项功能,如:印象笔记、支付宝、哔哩哔哩、IT之家、知乎、美团。于是自己怀着好奇,用动态代码也实现了一下,

1.设置ShortcutManager

@RequiresApi(api = Build.VERSION_CODES.N_MR1)
    private void setShortcutManager() {
        ShortcutManager mShortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);
        //方便扩展,写成数组的方式
        ShortcutInfo[] mShortcutInfos = new ShortcutInfo[]{getOrdinaryShortcut()};
        mShortcutManager.addDynamicShortcuts(Arrays.asList(mShortcutInfos));
    }

2.构造快捷方式信息

 @RequiresApi(api = Build.VERSION_CODES.N_MR1)
    private ShortcutInfo getOrdinaryShortcut() {
        ShortcutInfo ordinary = new ShortcutInfo.Builder(this, "guahao")
                .setIntents(new Intent[]{new Intent(Intent.ACTION_VIEW).
                        setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
                        .setClass(this, MainActivity.class),
                        new Intent(Intent.ACTION_VIEW).setClass(this,
                                OrdinaryYuYueActivity.class)}
                )
                .setShortLabel(getResources().getString(R.string.guahao))
                .setLongLabel(getResources().getString(R.string.guahao))
                .setIcon(Icon.createWithResource(this, R.mipmap.push))
                .build();
        return ordinary;
    }

3.判断版本号,在合适的地方调用

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            setShortcutManager();
        }

最终效果如图:


QQ图片20180531143129.jpg
上一篇 下一篇

猜你喜欢

热点阅读