Android创建桌面快捷方式

问题描述:

    想要在手机的桌面上添加快捷方式,而桌面又属于系统的应用,也就是说我们需要有一个与系统进行通信的接口。还好Android中有广播,而Android系统中又有接收添加快捷方式广播的广播接收者。于是,为我们的应用快捷方式就变得很简单了。

添加普通快捷方式:

private void createShortCut() {
        Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        
        // 获取快捷键的图标
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.short_cut_icon);
        Intent myIntent = new Intent(this, ShortCutActivity.class);
        
        // 快捷方式的标题
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");
        // 快捷方式的图标
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        // 快捷方式的动作
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);
        
        // 发送广播
        sendBroadcast(addIntent);
    }

添加带有数字标识的快捷方式:


而有时我们需要在快捷方式上添加一些标志信息,例如数字。下面就来看看怎么在图标上添加数字的提示信息吧。先来看看资源图片和数字合成后的图片函数:

private Bitmap generatorContactCountIcon(Bitmap icon, int count) {
        // 初始化画布
        int iconSize = (int) getResources().getDimension(android.R.dimen.app_icon_size);
        Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize,Config.ARGB_8888);
        Canvas canvas = new Canvas(contactIcon);

        // 拷贝图片
        Paint iconPaint = new Paint();
        iconPaint.setDither(true); // 防抖动
        iconPaint.setFilterBitmap(true); // 用来对Bitmap进行滤波处理,这样,当你选择Drawable时,会有抗锯齿的效果
        Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
        Rect dst = new Rect(0, 0, iconSize, iconSize);
        canvas.drawBitmap(icon, src, dst, iconPaint);

        // 启用抗锯齿和使用设备的文本字距
        Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        countPaint.setColor(Color.GREEN);
        countPaint.setTextSize(20f);
        countPaint.setTypeface(Typeface.DEFAULT_BOLD);
        canvas.drawText(String.valueOf(count), iconSize - 18, 25, countPaint);
        return contactIcon;
    }


这时添加快捷的实现会有一些修改:

private void installShortCut(int count) {
        Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name) + "-A");
        // 是否可以有多个快捷方式的副本,参数如果是true就可以生成多个快捷方式,如果是false就不会重复添加
        shortcutIntent.putExtra("duplicate", false);
        Intent mainIntent = new Intent(Intent.ACTION_MAIN);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        // 要删除的应用程序的ComponentName,即应用程序包名+activity的名字
        mainIntent.setClass(this, this.getClass());

        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent);
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable) (getResources().getDrawable(R.drawable.short_cut_ic_launcher))).getBitmap(), count));
        sendBroadcast(shortcutIntent);
    }


判断快捷方式是否存在:

private boolean hasShortcut() {
        boolean isInstallShortcut = false;
        final ContentResolver cr = getContentResolver();
        final String AUTHORITY = "com.android.launcher2.settings";
        final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                + "/favorites?notify=true");
        Cursor c = cr.query(CONTENT_URI,
                new String[] { "title", "iconResource" }, "title=?",
                new String[] { "快捷方式" }, null);
        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }
        return isInstallShortcut;
    }

快捷方式的删除:

注:以下的删除方法其实是不可用的,具体原因还在找。。。如果有谁知道是为什么可以留言告诉我,谢谢

public void deleteShortCut(int index) {
        Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");

        Parcelable icon = Intent.ShortcutIconResource.fromContext(this, Icons.ICON_RES[index]);
        Intent myIntent = new Intent(this, MainActivity.class);

        // 快捷方式的标题
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式" + index);
        // 快捷方式的图标
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
        // 快捷方式的动作
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);

        sendBroadcast(shortcut);

        Toast.makeText(MainActivity.this, "del...", 0).show();
    }


添加权限:

    <!-- 创建快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    
    <!-- 删除快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
    
    <!-- 验证快捷方式是否存 -->
    <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
    
    <!-- 更改主屏幕中的设置和快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />

笔者最新开发的应用连接

http://zhushou.360.cn/detail/index/soft_id/2419729?recrefer=SE_D_%E6%A3%B1%E9%95%9C

http://www.wandoujia.com/apps/com.mastershield.personalstat


原文地址:https://www.cnblogs.com/fengju/p/6336126.html