Android 8.0 快捷方式Shortcut

Oreo创建app快捷方式两种方式:
v7包:appcompat-v7:26.0.2
ShortcutManager requestPinShortcut()
LauncherActivity:点击快捷方式启动的Activity
shortcutId:快捷方式id
bitmap:Shortcut图标
shortcutTitle:Shortcut名称
注意: 如果快捷方式已存在,则ShortcutInfo对象应仅包含快捷方式的ID。否则,新的ShortcutInfo对象必须包含新快捷方式的ID,意图和短标签。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
           //1
           ShortcutManager shortcutManager = (ShortcutManager) mContext.getSystemService(Context.SHORTCUT_SERVICE);
           if (shortcutManager.isRequestPinShortcutSupported()) {
               Intent launcherIntent= new Intent(mContext, LauncherActivity.class);
               launcherIntent.setAction(Intent.ACTION_VIEW);
               ShortcutInfo info = new ShortcutInfo.Builder(mContext, shortcutId)
                       .setIcon(Icon.createWithBitmap(bitmap))
                       .setShortLabel(shortcutTitle)
                       .setIntent(launcherIntent)
                       .build();
               //当添加快捷方式的确认弹框弹出来时,将被回调
               PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(mContext, ShortcutReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
               shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
           }
           //2
           if (ShortcutManagerCompat.isRequestPinShortcutSupported(mContext)) {
               Intent launcherIntent = new Intent(mContext, LauncherActivity.class);
               launcherIntent.setAction(Intent.ACTION_VIEW);
               ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(mContext, shortcutId)
                       .setIcon(bitmap)
                       .setShortLabel(shortcutTitle)
                       .setIntent(launcherIntent)
                       .build();

               //当添加快捷方式的确认弹框弹出来时,将被回调
               PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(mContext, ShortcutReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
               ShortcutManagerCompat.requestPinShortcut(mContext, info, shortcutCallbackIntent.getIntentSender());
           }

ShortcutReceiver:回调
清单声明的接收器来接收回调,android:exported="false"

public class ShortcutReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}
/**
     * Android 7.1及以下 添加桌面
     */
    public static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";

    public void addShortcutBelowAndroidN(Context context) {
        Intent addShortcutIntent = new Intent(ACTION_ADD_SHORTCUT);

        // 不允许重复创建,不是根据快捷方式的名字判断重复的
        addShortcutIntent.putExtra("duplicate", false);

        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name");

        //图标
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.mipmap.ic_shortcut));

        // 设置关联程序
        Intent launcherIntent = new Intent();
        launcherIntent.setClass(context, ShortcutActivity.class);
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,826评论 25 709
  • 历经一年多的开发和数月以来开发者与早期用户的反复测试,Android 8.0 Oreo 终于正式面向全球发布。An...
    谷歌开发者阅读 8,107评论 35 87
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 龙之谷手游体验报告 by oasisX 一、说明 游戏名称:龙之谷手游 游戏版本:1.13.0 设备型号:Ipho...
    oasisX阅读 2,300评论 0 4
  • 1·拓宽你的选择范围 “是否”型决定/表明决心式- 导致思维狭隘,选项太少。 提问式:我想从生活中得到什么?哪些选...
    第十二颗星球阅读 165评论 0 0