开源项目之Android AnyCut(快捷方式)

AnyCut创建快捷方式,可以创建N个快捷方式,例如可以吧喜欢的网页保存在桌面上,方便下次打开,也可以把电话号码保存在桌面上,方便打电话。就像电脑上的快捷方式一样。很好的实例,下载下来直接就可以使用。项目如图:


效果如图:



关键源码贴出:

    private Intent createShortcutIntent() { //创建快捷意图
        Intent intent = new Intent();

        EditText view;
        view = (EditText) findViewById(R.id.action);
        intent.setAction(view.getText().toString());

        view = (EditText) findViewById(R.id.data);
        String data = view.getText().toString();
        view = (EditText) findViewById(R.id.type);
        String type = view.getText().toString();

        boolean dataEmpty = TextUtils.isEmpty(data);
        boolean typeEmpty = TextUtils.isEmpty(type);
        if (!dataEmpty && typeEmpty) {
            intent.setData(Uri.parse(data));
        } else if (!typeEmpty && dataEmpty) {
            intent.setType(type);
        } else if (!typeEmpty && !dataEmpty) {
            intent.setDataAndType(Uri.parse(data), type);
        }

        return new Intent().putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
    }
    //返回一个Intent描述了快捷键意图
    private Intent generatePhoneShortcut(Intent result, int actionResId, String scheme, String action) { 
        Uri phoneUri = result.getData();
        long personId = 0;
        String name = null;
        String number = null;
        int type;
        Cursor cursor = getContentResolver().query(phoneUri,
                new String[] { Phones.PERSON_ID, Phones.DISPLAY_NAME, Phones.NUMBER, Phones.TYPE },
                null, null, null);
        try {
            cursor.moveToFirst();
            personId = cursor.getLong(0);
            name = cursor.getString(1);
            number = cursor.getString(2);
            type = cursor.getInt(3);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        Intent intent = new Intent();
        Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,
                generatePhoneNumberIcon(personUri, type, actionResId));

        // Make the URI a direct tel: URI so that it will always continue to work
        phoneUri = Uri.fromParts(scheme, number, null);
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(action, phoneUri));
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
        return intent;
    }

    //生成一个电话号码的快捷方式图标
    private Bitmap generatePhoneNumberIcon(Uri personUri, int type, int actionResId) {
        final Resources r = getResources();
        boolean drawPhoneOverlay = true;

        Bitmap photo = People.loadContactPhoto(this, personUri, 0, null);
        if (photo == null) {
            // If there isn't a photo use the generic phone action icon instead
            Bitmap phoneIcon = getPhoneActionIcon(r, actionResId);
            if (phoneIcon != null) {
                photo = phoneIcon;
                drawPhoneOverlay = false;
            } else {
                return null;
            }
        }

        // Setup the drawing classes
        int iconSize = (int) r.getDimension(android.R.dimen.app_icon_size);
        Bitmap icon = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(icon);

        // Copy in the photo
        Paint photoPaint = new Paint();
        photoPaint.setDither(true);
        photoPaint.setFilterBitmap(true);
        Rect src = new Rect(0,0, photo.getWidth(),photo.getHeight());
        Rect dst = new Rect(0,0, iconSize,iconSize);
        canvas.drawBitmap(photo, src, dst, photoPaint);

        // Create an overlay for the phone number type
        String overlay = null;
        switch (type) {
            case Phones.TYPE_HOME:
                overlay = "H";
                break;

            case Phones.TYPE_MOBILE:
                overlay = "M";
                break;

            case Phones.TYPE_WORK:
                overlay = "W";
                break;

            case Phones.TYPE_PAGER:
                overlay = "P";
                break;

            case Phones.TYPE_OTHER:
                overlay = "O";
                break;
        }
        if (overlay != null) {
            Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
            textPaint.setTextSize(20.0f);
            textPaint.setTypeface(Typeface.DEFAULT_BOLD);
            textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
            textPaint.setShadowLayer(3f, 1, 1, r.getColor(R.color.textColorIconOverlayShadow));
            canvas.drawText(overlay, 2, 16, textPaint);
        }

        // Draw the phone action icon as an overlay
        if (ENABLE_ACTION_ICON_OVERLAYS && drawPhoneOverlay) {
            Bitmap phoneIcon = getPhoneActionIcon(r, actionResId);
            if (phoneIcon != null) {
                src.set(0,0, phoneIcon.getWidth(),phoneIcon.getHeight());
                int iconWidth = icon.getWidth();
                dst.set(iconWidth - 20, -1, iconWidth, 19);
                canvas.drawBitmap(phoneIcon, src, dst, photoPaint);
            }
        }

        return icon;
    }

创建后,看下主界面,你会发现多了一个快捷图标!呵呵,代码是不是非常简单易懂?~

项目下载


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值