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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章