当前位置 : 主页 > 编程语言 > c++ >

ShortcutBuilder

来源:互联网 收集:自由互联 发布时间:2021-07-03
快捷方式 package com.dualapp.main.plus;import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;public class ShortcutBuilder { private static final String ACTION_ADD_SHORTCUT = "com.android.launcher.ac
快捷方式
package com.dualapp.main.plus;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;

public class ShortcutBuilder {

    private static final String ACTION_ADD_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT";
    private static final String ACTION_REMOVE_SHORTCUT = "com.android.launcher.action.UNINSTALL_SHORTCUT";

    private Context mContext;
    private Intent mIntent;

    public ShortcutBuilder(Context context) {
        mContext = context;
        mIntent = new Intent();
        mIntent.putExtra("duplicate", false); // 不允许重复创建
    }

    public ShortcutBuilder intent(Intent intent) {
        mIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        return this;
    }

    public ShortcutBuilder label(int label) {
        mIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mContext.getString(label));
        return this;
    }

    public ShortcutBuilder label(String label) {
        mIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, label);
        return this;
    }

    public ShortcutBuilder icon(Bitmap icon) {
        mIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, icon);
        mIntent.removeExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE);
        return this;
    }

    public ShortcutBuilder icon(Intent.ShortcutIconResource iconRes) {
        mIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
        mIntent.removeExtra(Intent.EXTRA_SHORTCUT_ICON);
        return this;
    }

    public ShortcutBuilder icon(String packageName) {
        return icon(packageName, 0);
    }

    public ShortcutBuilder icon(String packageName, int icon) {
        Intent.ShortcutIconResource iconRes = null;
        try {
            if (icon == 0) {
                icon = mContext.getPackageManager().getApplicationInfo(packageName, 0).icon;
            }
            Context weixin = mContext.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
            iconRes = Intent.ShortcutIconResource.fromContext(weixin, icon);
        } catch (Exception e) {
            return this;
        }
        return icon(iconRes);
    }

    public ShortcutBuilder enableDuplicate() {
        mIntent.putExtra("duplicate", true);
        return this;
    }

    public boolean add() {
        mIntent.setAction(ACTION_ADD_SHORTCUT);
        try {
            mContext.sendBroadcast(mIntent);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean remove() {
        mIntent.setAction(ACTION_REMOVE_SHORTCUT);
        try {
            mContext.sendBroadcast(mIntent);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
网友评论