Android 7.1 ShortcutManager(应用快捷方式)的超简单使用及一些可能遇到的BUG

最近开发中,需要为应用适配上快捷方式,然后遇到了快捷方式的语言不能随着系统本地语言切换而改变的BUG,这里就顺便简单记录下 ShortcutManager 的使用吧。


首先简单解释下什么是 ShortcutManager(应用快捷方式):这是在Android 7.1后添加的新功能,即长按Launcher 桌面上的APP图标,会根据你项目中编写的相应代码,显示一列快捷方式图标。点击不同的快捷方式即能快速进入APP中相应的Activity,同时长按拖动快捷方式,还可以在Launcher 桌面上添加相应快捷方式的图标。


实现方式有两种:

1.静态方式,即在Manifest 和 shortcuts.xml 中进行相关配置就ok了。

2.动态方式,在代码中通过 ShortcutManager 进行相关操作,来动态配置快捷方式。

这里有一点需要注意的是:一个APP是可以同时通过静态与动态两种方式生成应用快捷方式的,最终应用快捷方式的列表中会将两种方式生成的都展现出来。

但是,动态与静态生成快捷方式的 shortcutId 一定不能是一样的,否则会报 java.lang.IllegalArgumentException: Manifest shortcut ID=xxx may not be manipulated via APIs 的错误,即你不能动态去修改静态xml中已经配置好的快捷方式。


静态方式

在res/xml下新建 shortcuts_static.xml,进行相关配置即可:

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ts_shortcut_local_theme"
        android:shortcutId="static_local_theme"
        android:shortcutLongLabel="@string/title_local_theme"
        android:shortcutShortLabel="@string/title_local_theme">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.xxx.ThemeActivity"
            android:targetPackage="com.xxx.themestore" />

        <categories android:name="android.shortcut.conversation" />
    </shortcut>
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ts_shortcut_local_wallpaper"
        android:shortcutId="static_local_wallpaper"
        android:shortcutLongLabel="@string/title_local_wallpaper"
        android:shortcutShortLabel="@string/title_local_wallpaper">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.xxx.WallpaperActivity"
            android:targetPackage="com.xxx.themestore" />

        <categories android:name="android.shortcut.conversation" />
    </shortcut>
</shortcuts>

一目了然很简单,具体属性的含义待会儿在动态方式中会提及,shortcuts_static.xml 编写完后,再在AndroidManifest.xml中的启动Activity进行相关申明:

        <activity
            android:name="com.xxx.LoadingActivity"
            android:label="${app_name}"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            //添加快捷方式
            <meta-data
                android:name="android.app.shortcuts"
                android:resource="@xml/shortcuts_static"
                >
            </meta-data>
        </activity>

ok,至此通过静态方式生成APP桌面快捷方式就完成了。


动态方式

开篇就曾说过,遇到了快捷方式的语言不能随着系统本地语言切换而改变的BUG,对!静态方式是没问题的,就是通过动态方式添加时遇到了这个BUG!我这里尝试了半天,最终只想出了注册一个静态广播,监听系统语言的切换,然后动态更新快捷方式的办法。虽然感觉这应该并不是最优解,但好歹是暂且解决了,各位大神有更好的办法的话,请赐教~~~

在AndroidManifest中注册一个监听广播:

        <receiver android:name=".manager.LocaleReceiver">
            <intent-filter>
                <action android:name="android.intent.action.LOCALE_CHANGED"/>
            </intent-filter>
        </receiver>

动态生成快捷方式:

public class LocaleReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            if (Build.VERSION.SDK_INT >= 25) {
                Log.e("sheep", intent.toString());
                ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class);
                Intent intent_theme = new Intent(context, ThemeActivity.class);
                intent_theme.setAction(Intent.ACTION_VIEW);
                intent_theme.putExtra("islocal", true);
                ShortcutInfo shortcut_theme = new ShortcutInfo.Builder(context, "local_theme")
                        .setShortLabel(context.getString(R.string.title_local_theme))
                        .setLongLabel(context.getString(R.string.title_local_theme))
                        .setIcon(Icon.createWithResource(context, R.drawable.ts_shortcut_local_theme))
                        .setIntent(intent_theme)
                        .build();
                Intent intent_wallpaper = new Intent(context, WallpaperActivity.class);
                intent_wallpaper.setAction(Intent.ACTION_VIEW);
                intent_wallpaper.putExtra("islocal", true);
                ShortcutInfo shortcut_wallpaper = new ShortcutInfo.Builder(context, "local_wallpaper")
                        .setShortLabel(context.getString(R.string.title_local_wallpaper))
                        .setLongLabel(context.getString(R.string.title_local_wallpaper))
                        .setIcon(Icon.createWithResource(context, R.drawable.ts_shortcut_local_wallpaper))
                        .setIntent(intent_wallpaper)
                        .build();
//                shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut_wallpaper, shortcut_theme));
                shortcutManager.updateShortcuts(Arrays.asList(shortcut_wallpaper, shortcut_theme));
            }
        } catch (Exception e) {

        }
    }
}

其中 setShortLabel( ) 为将快捷方式拖动到桌面在Launcher上生成的快捷方式图标时的名称,而 setLongLabel( ) 则为长按APP 时显示的一列快捷方式中的名称,updateShortcuts( ) 为更新快捷方式,shortcutManager.setDynamicShortcuts( ) 则为第一次动态创建时使用,其实 ShortcutManager 这个类的使用非常简单,还有一些 API 这里就不一一列出了,大家有需要的可以去官方文档中查阅。


至此,ShortcutManager (应用快捷方式)就完成了,最后来看一看效果:

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值