Android 在Launcher桌面添加应用快捷图标(适用于Android 7.0(API24) 及以下)

Launcher为了应用程序能够定制自己的快捷图标,就注册了一个 BroadcastReceiver 专门接收其他应用程序发来的快捷图标定制信息。所以只需要根据该BroadcastReceiver构造出相对应的Intent并装入我们的定制信息,最后调用 sendBroadcast 方法就可以创建一个快捷图标了。一般创建应用快捷图标有如下两种方案:

方案1:在长按桌面弹出的WIDGETS中手动选择添加

这里写图片描述

首先在应用的AndroidManifest.xml中需要创建快捷方式的Activity中增加如下action:

        <activity android:name=".AppShortcuts.AppShortcutsDemoActivity">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
            </intent-filter>
        </activity>

接下来在刚才的Activity的onCreate()方法中添加如下代码:

        if (getIntent()!=null && getIntent().getAction()!=null){
            if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) {
            Intent _returnIntent = new Intent();
            _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "studyDemo_shortcut");
            _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.logo));
            _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this, MainActivity.class));
            setResult(RESULT_OK, _returnIntent);
            finish();
            }
        }

然后长按桌面弹出的WIDGETS中就可以看到我们添加的应用了


方案2:在代码中动态的向桌面添加/删除 应用快捷方式注意:

注意:针对Android 7.0(API24)及以下有效,在7.1及以上的版本中需要使用ShortcutManager的相关方法。若在Android8.0 及以上的系统中调用方案2中的方法时,会打印如下信息:
ActivityManager: Broadcast com.android.launcher.action.INSTALL_SHORTCUT no longer supported. It will not be delivered.
这里写图片描述

首先需要添加权限

    <!-- 添加快捷方式 -->
    <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.launcher2.permission.READ_SETTINGS"/>
    <uses-permission android:name="com.android.launcher3.permission.READ_SETTINGS"/>

关键代码:

    /**
     * 添加当前应用的桌面快捷方式
     * 
     * @param context
     */
    public static void addShortcut(Context context, int appIcon) {
        Intent shortcut = new Intent(
                "com.android.launcher.action.INSTALL_SHORTCUT");

        Intent shortcutIntent = context.getPackageManager()
                .getLaunchIntentForPackage(context.getPackageName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        // 获取当前应用名称
        String title = null;
        try {
            final PackageManager pm = context.getPackageManager();
            title = pm.getApplicationLabel(
                    pm.getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA)).toString();
        } catch (Exception e) {
        }
        // 快捷方式名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        // 不允许重复创建(不一定有效)
        shortcut.putExtra("duplicate", false);
        // 快捷方式的图标
        Parcelable iconResource = Intent.ShortcutIconResource.fromContext(context,
                appIcon);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

        context.sendBroadcast(shortcut);
    }

    /**
     * 删除当前应用的桌面快捷方式
     * 在android7.0 上测试无效
     * @param context
     */
    public static void delShortcut(Context context) {
        Intent shortcut = new Intent(
                "com.android.launcher.action.UNINSTALL_SHORTCUT");

        // 获取当前应用名称
        String title = null;
        try {
            final PackageManager pm = context.getPackageManager();
            title = pm.getApplicationLabel(
                    pm.getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA)).toString();
        } catch (Exception e) {
        }
        // 快捷方式名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
        Intent shortcutIntent = context.getPackageManager()
                .getLaunchIntentForPackage(context.getPackageName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        context.sendBroadcast(shortcut);
    }
    /**
     * 判断当前应用在桌面是否有桌面快捷方式
     * 
     * @param context
     */
    public static boolean hasShortcut(Context context) {
        boolean result = false;
        String title = null;
        try {
            final PackageManager pm = context.getPackageManager();
            title = pm.getApplicationLabel(
                    pm.getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA)).toString();
        } catch (Exception e) {

        }

        final String uriStr;
        if (android.os.Build.VERSION.SDK_INT < 8) {
            uriStr = "content://com.android.launcher.settings/favorites?notify=true";
        } else if (android.os.Build.VERSION.SDK_INT < 19) {
            uriStr = "content://com.android.launcher2.settings/favorites?notify=true";
        } else {
            uriStr = "content://com.android.launcher3.settings/favorites?notify=true";
        }
        final Uri CONTENT_URI = Uri.parse(uriStr);
        final Cursor c = context.getContentResolver().query(CONTENT_URI, null,
                "title=?", new String[]{title}, null);
        if (c != null && c.getCount() > 0) {
            result = true;
        }
        return result;
    }

猜你喜欢

转载自blog.csdn.net/makercloud/article/details/80841781