4

Android 生成桌面快捷方式是这样做的

 2 years ago
source link: http://www.androidchina.net/10161.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

关于生成桌面快捷方式,Android提供了原生的api方法。下边就介绍一下兼容8.0的具体做法。

一、首先当然是关于快捷方式的权限了。

我们需要在AndroidManifest文件中添加一下权限:

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

二、在需要生成桌面快捷方式处调用一下代码

 /**
     * 添加快捷方式
     */
    public void addShortCutCompact(Bitmap bitmap) {
        //启动器是否支持添加快捷方式
        if (ShortcutManagerCompat.isRequestPinShortcutSupported(mContext)) {
            Intent shortcutInfoIntent = new Intent(mContext, TestActivity.class);
             //这里直接传一个对象报错,现在转成json传过去
            Gson gson = new Gson();
            String json = gson.toJson(ShortcutBean);
            shortcutInfoIntent.putExtra(SHORTCUT_BEAN,json );
            shortcutInfoIntent.putExtra(IS_SHORTCUT, true);
            //action必须设置,不然报错
            shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
            shortcutInfoIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutInfoIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(mContext, "mama_id")
                    //设置图标icon
                    .setIcon(Icon.createWithResource(context, R.drawable.icon))
                    //设置名称
                    .setShortLabel("快捷方式名称")
                    .setIntent(shortcutInfoIntent)
                    .build();

            //创建快捷方式
            ShortcutManagerCompat.requestPinShortcut(mContext, info,null);

        } else {
            ToastUtil.showMsg("启动器不支持固定快捷方式");
        }
    }

三、注意点

1、点击桌面快捷方式跳转到目标页面就是上边代码例子中的TestActivity时,如果有业务需要做一些统计之类的操作,我们可以传递一些数据进行操作,这里可以直接传递基本数据类型,但是如果想直接传递一个bean对象时,是不支持的,需要我们转成json传递。

2、shortcutInfoIntent.setAction(Intent.ACTION_VIEW);是必须设置的。

3、桌面快捷方式的图标不支持网络地址图片,如果想要从接口获取动态设置需要先下载下来为一个bitmap。用setIcon(IconCompat.createWithBitmap(bitmap))方法来设置。 关于下载网络图片我们有很多方法,这里是用glide下载,列子代码:

 /**
     *  下载快捷方式icon
     */
    private void downShortcutICon() {
        final Bitmap[] bitmap = new Bitmap[1];
        //先下载图标 转为bitMap
        Glide.with(mContext).asBitmap().load(addIcon.getAppIcon()).into(new SimpleTarget() {
            @Override
            public void onResourceReady(@NonNull Object resource, @Nullable Transition transition) {

                bitmap[0] = (Bitmap) resource;
                if (bitmap[0] != null) {
                 	addShortCutCompact(bitmap[0]);
                }

            }

        });
    }

4、需要在AndroidManifest中的TestActivity注册中加入 android:exported=”true”标签,这个标签是用来表示是否能够被其他应用程序组件调用或跟它交互。

5、另外,如小米、vivo等一些机型需要在设置中手动打开 创建桌面快捷方式权限。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK