Oreo创建app快捷方式

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);
    }

链接:https://www.jianshu.com/p/c3b862279e38

猜你喜欢

转载自blog.csdn.net/gezi322/article/details/105858669