android8.0桌面快捷方式,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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值