Android OPPO 手机桌面快捷方式遇到的问题

2018-08-07  本文已影响326人  風清雲少

关于快捷方式大家都知道,每个厂商多多少少都有些奇奇怪怪的问题。下面自己遇到的两个问题,
OPPO A57手机 和 8.0 系列手机(小米Mix 2S)

首先Manifest权限

<uses-permission android:name="com.oppo.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.oppo.launcher.permission.WRITE_SETTINGS" />

关于创建删除更新的操作我就不贴代码了,网上都有都差不多,直接进入关键部分。

在logcat 发现一行神奇的日志

Launcher.LauncherModel: the shortcut has a launcher category, but no extras or data, just return!! intent = Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]

       Intent addShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");

        // 不允许重复创建
        addShortcutIntent.putExtra("duplicate", false);// 经测试不是根据快捷方式的名字判断重复的
        // 应该是根据快链的Intent来判断是否重复的,即Intent.EXTRA_SHORTCUT_INTENT字段的value
        // 但是名称不同时,虽然有的手机系统会显示Toast提示重复,仍然会建立快链
        // 屏幕上没有空间时会提示
        // 注意:重复创建的行为MIUI和三星手机上不太一样,小米上似乎不能重复创建快捷方式

        // 名字
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.scan_open_shortcut));

        // 图标
        addShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(context,
                        R.mipmap.ic_launcher_shortcut));

        // 设置关联程序
        Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
        launcherIntent.setClass(context, ScanActivity.class);
        launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
      // 按照logcat 上提示信息添加一个数据。
        launcherIntent.putExtra("data","看这里");
        addShortcutIntent
                .putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);

        // 发送广播
        context.sendBroadcast(addShortcutIntent);

在设置关联Activity 的Intent 上 添加一个数据,然后就可以创建快捷方式了。从日志上来看很明显系统判断没有携带数据就直接return 了。至于为什么有这么个操作,没有去看源码深究这个问题。如果有小伙伴知道相关信息可以分享给我。(其实我就是懒(:з」∠)

下面是关于Android 8.0 以上创建快捷方式

从Android 7.1(API 25)开始,新增了ShortcutManager,可以对桌面久按应用图标弹出的快捷方式进行管理。
我也是网上搜到别人分享的文章,就不抄过来了。大家跳转过去看吧。

https://blog.csdn.net/rentee/article/details/77005547

上一篇 下一篇

猜你喜欢

热点阅读