创建桌面快捷方式

QuibblerQuibbler 2020-05-19 约 13 分钟 2809 次阅读

创建桌面快捷方式

创建桌面快捷方式,方便用户直达页面。比如微信的联系人快捷方式,点击直接进入聊天界面,省去很多步骤。Android API升级过程中也对创建桌面快捷方式的方式进行了改进。有两种创建桌面快捷方式的途径,详见第二和第三节,开发中要做好兼容,这就是安卓最麻烦的地方。

1、所需权限

创建桌面快捷方式也需要声明权限,不然不能正确在桌面创建快捷方式。

<!-- 添加快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    
    <!-- 移除快捷方式 -->
    <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

2、Android N之前创建快捷方式(废弃)

Android 7.1(N,API 25)推出了ShortcutManager管理图标触摸菜单和快捷方式,到此阶段,都可以通过Intent广播创建桌面快捷方式。

先初始化Intent,action必须为com.android.launcher.action.INSTALL_SHORTCUT

Intent createShortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    
    // 是否允许重复创建相同的快捷方式,不是根据快捷方式的名字判断重复的
    createShortcutIntent.putExtra("duplicate", false);
    //设置快捷方式名称
    createShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式名");
    //图标
    createShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
                                  Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
    // 设置桌面快捷方式的启动Intent
    Intent startIntent = new Intent();
    startIntent.setClass(context, 启动的Activity.class);
    createShortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, startIntent);
    
    // 发送广播
    context.sendBroadcast(createShortcutIntent);

附:与之相关的几个ACTION都定义在Intent类中,不过这种方式以及被废弃

public static final String ACTION_CREATE_SHORTCUT = "android.intent.action.CREATE_SHORTCUT";
    
    /**
     * 用于定义快捷方式意图的附加项的名称。
     */
    @Deprecated
    public static final String EXTRA_SHORTCUT_INTENT = "android.intent.extra.shortcut.INTENT";
    /**
     * 附加项的名称,用于定义快捷方式的名称。
     */
    @Deprecated
    public static final String EXTRA_SHORTCUT_NAME = "android.intent.extra.shortcut.NAME";
    /**
     * 用于将图标定义为快捷方式的附加项目的名称。
     */
    @Deprecated
    public static final String EXTRA_SHORTCUT_ICON = "android.intent.extra.shortcut.ICON";
    /**
     * 用于将图标定义为快捷方式的ShortcutIconResource的附加名称。
     */
    @Deprecated
    public static final String EXTRA_SHORTCUT_ICON_RESOURCE = "android.intent.extra.shortcut.ICON_RESOURCE";

3、Android O之后创建快捷方式

但Android 8.0(O,API 26)开始只能通过ShortcutManager创建和管理桌面快捷方式。

3.1、ShortcutManager创建快捷方式

获取系统Service的方式获取ShortcutManager对象:

ShortcutManager shortcutManager = (ShortcutManager) getSystemService(Context.SHORTCUT_SERVICE);

if (shortcutManager.isRequestPinShortcutSupported()) {
        Intent shortcutInfoIntent = new Intent(context, SecondActivity.class);
        shortcutInfoIntent.setAction(Intent.ACTION_VIEW);
        ShortcutInfo info = new ShortcutInfo.Builder(context, "快捷方式的ID")
                .setIcon(Icon.createWithResource(context, R.drawable.icon))
                .setShortLabel("快捷方式名")
                .setIntent(shortcutInfoIntent)
                .build();
                
        //添加快捷方式的确认弹框弹出来时,将被回调。这个因系统而异。
        PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), 
                                                                          PendingIntent.FLAG_UPDATE_CURRENT);
        shortcutManager.requestPinShortcut(info, shortcutCallbackIntent.getIntentSender());
    }

3.2、创建快捷方式的系统差异

国内各大厂商对系统的深度定制导致安卓的标准API在不同的厂商手机上运行有不同的行为,这很正常。

①比如vivo的手机,一般三方应用是没有创建桌面快捷方式的权限(连微信也不能正常创建联系人快捷方式),默认关闭,需要手动打开或者跳转到设置界面提示用户打开。

②而且不会有弹框,像标准的Google设备以及OPPO手机用这种方式创建会有弹框提示:

③锤子手机更神奇,点击桌面快捷方式。如果应用已经打开过,在后台,只是把应用调到前台,而不是打开指定的Intent中设置的Activity页面。

4、ShortcutManagerCompat兼容(推荐)

适配不同的系统可以使用,但是比较麻烦,好在官方又又推荐了一个创建桌面快捷方式的AndroidX兼容库的。用ShortcutManagerCompat进行快捷方式的版本适配。

4.1、添加依赖

//ShortcutManagerCompat
    implementation 'androidx.appcompat:appcompat:1.1.0'

4.2、使用兼容包

使用ShortcutManagerCompat实现不同安卓系统版本创建桌面快捷方式的兼容。

if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
        Intent shortcutInfoIntent = new Intent(context, 快捷方式打开的Activity.class);
        //action必须设置,不然报错
        shortcutInfoIntent.setAction(Intent.ACTION_VIEW); 
        ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(context, "快捷方式的ID")
                .setIcon(IconCompat.createWithResource(context, R.drawable.icon))
                .setShortLabel("快捷方式名")
                .setIntent(shortcutInfoIntent)
                .build();
                
        //当添加快捷方式的确认弹框弹出来时,将被回调
        PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, MyReceiver.class), 
                                                                          PendingIntent.FLAG_UPDATE_CURRENT);
        ShortcutManagerCompat.requestPinShortcut(context, info, shortcutCallbackIntent.getIntentSender());
    }

参考资料:

        Android创建桌面快捷方式(兼容Android 8.0)

        Android中的桌面快捷图标

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

        Android 仿微博,探索使用ShortcutManager添加应用程序的快捷方式

        Android 7.1 应用快捷方式(ShortcutManager使用)

Android笔记:Android 在桌面生成快捷方式(仿微信)

相关推荐

精选
ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager
Android

ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager

ViewPager1、ViewPagerandroidx.viewpager.widget.ViewPager,Android中使用非常广泛的控件,可以说是APP必备:首次打开引导页、页面Banner广告等。常用方法:setAdapter() 设置适配器setOffscreenPageLimit() 设置缓存的页面个数,默认是 1setCurrentItem() 跳转到特定的页面setOnPage

1.1k
获取Android内置WebView内核版本
Android

获取Android内置WebView内核版本

获取Android内置WebView内核版本竟然能遇到这样奇葩的事情,网页用的技术过于新颖,以至于只支持高版本Chromium内核的,低版本安卓系统中内置的内核版本较低,无法加载前端页面。1、设置查看在系统设置里 > 应用 > 应用管理 > 显示系统应用,查看WebView组件:2、页面查看通过WebView发起的网络请求,都会带上浏览器的UA,通常页面都可以通过UA判断浏览器的内核版本。这里有两

1.1w
Android 14适配总结
Android

Android 14适配总结

Android 14适配总结毕业工作至今已经适配了三个Android大版本,从Android 11到Android 12、再到Android 13。2023年,Google即将推出的Android 14,上半年已经开始第一批适配。现在,第四个Android版本已经适配完,总结记录一下。1、Android 14计划Google一般会在2月份对外发布预告,同时放出开发者预览版。“拉通”各大平台、厂商以

1.1w