Android 应用程序快捷方式相关

 Android 7.0后应用可以在启动图标上添加快捷方式,以便更快捷的操作。

 这篇文章主要讲的就是相关的用法。

首先看下效果,再说用法。

如图,当长按启动logo时,除了卸载,信息,多除了一个快捷栏列表。

1:创建快捷方式

总的来说一共有三种:

静态创建方式:清单中写死,适用于用户与程序在整个生命周期内使用一致结构链接到内容的应用程序。

动态创建方式:用于上下文敏感的应用,允许用户每次清除时更新快捷方式。

固定创建方式:用于特定的用户驱动操作,例如用户将特定的操作固定到驱动器。

快捷方式可以一次为应用程序发布最多五个快捷方式(静态和动态快捷方式组合),但大多数启动器只能显示四个。

1.1:静态创建

    第一步:查找android.intent.action.MAIN和android.intent.category.LAUNCHER的filter的activity。

    只有处理Intent.ACTION_MAIN 操作和 Intent.CATEGORY_LAUNCHER 类别的主要活动 - 活动 才能有快捷方式。

   第二步:在该activity下添加以下代码:

<meta-data
 android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />

  第三步:在res下添加xml文件夹,并创建shortcuts.xml文件。

  第四步:编写shortcuts文件。

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:shortcutId="intent_user"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/shortcut_user_short_label"
        android:shortcutLongLabel="@string/shortcut_user_long_label"
        android:shortcutDisabledMessage="@string/shortcut_user_disable_label">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.zh.shortcuts"
            android:targetClass="com.zh.shortcuts.UserInfoActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>

    <shortcut
        android:shortcutId="intent_web"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutShortLabel="@string/shortcut_web_short_label"
        android:shortcutLongLabel="@string/shortcut_web_long_label"
        android:shortcutDisabledMessage="@string/shortcut_web_disable_label">

        <intent
            android:action="android.intent.action.VIEW"
            android:targetPackage="com.zh.shortcuts"
            android:targetClass="com.zh.shortcuts.WebActivity" />
        <categories android:name="android.shortcut.conversation"/>
    </shortcut>
</shortcuts>

  xml的根元素是<shortcuts> ,其下是<shortcut>元素列表,代表了快捷方式的目录。

  接着我们看下元素下的各个属性:

  shortcutId:字符串文字,不能是@string/xxx.的资源字符串。

  enabled:是否可以从支持的启动器与快捷方式进行交互,默认值是true。如果将其设置成false,则设置 shortcutDisabledMessage解释禁用快捷方式的原因。

  icon:menu的图标。可以使图片路径。也可以是资源id。

  shortcutShortLabel:描述快捷方式目的的简明短语,长度限制10个字符。必须是资源字符串,如:@string/xxx

  shortcutLongLabel:描述快捷方式目的的扩展短语,长度限制25个字符,这个就是快捷方式显示的menu名称。必须是资源字符串,如:@string/xxx

  shortcutDisabledMessage:当用户尝试启动已禁用的快捷方式时,支持的启动程序中显示的消息。必须是资源字符串,如:@string/xxx,当enable=true时,无效。

 

至于intent和categories我们已经很熟悉了,就不在赘述。

注意的是:categories直接写死 <categories android:name="android.shortcut.conversation"/>就行了,o(╥﹏╥)o

 

1.2动态创建

首先需要用到ShortcutManager这个类,来管理shortcut。

怎么用呢?

第一步:创建ShortcutManager对象

 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

接着我们看下这个类具体有哪些方法:

如图所示、基本上包含了列表,添加,修改这些操作。

另外我们看到集合中的对象是ShortcutInfo。

可以看到ShortcutInfo继承Parcelable,通过ShortcutManager来发布快捷方式。

那么第二步就简单了,创建ShortcutInfo对象:

  ShortcutInfo shortcut =  new ShortcutInfo.Builder(this, "create"+list.size())
                    .setShortLabel("个人主页"+list.size())
                    .setLongLabel("个人主页"+list.size())
                    .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
                    .setIntent(intent)
                    .build();

 简单的了解下,shortcutInfo通过build方式创建。

首先builder中参数上下文以及shortcutId:

  /**
         * Constructor.
         *
         * @param context Client context.
         * @param id ID of the shortcut.
         */
        public Builder(Context context, String id) {
            mContext = context;
            mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
        }

setShortLabel等属性,就是简单的设置,不在赘述。

其中,intent创建时必须添加action属性,而且必须是ACTION_VIEW

    Intent intent=new Intent(this,UserInfoActivity.class);
            intent.putExtra("msg","这是个人主页快捷方式进入"+list.size());
            intent.setAction(Intent.ACTION_VIEW);

第三步:添加shortcut

 shortcutManager.addDynamicShortcuts(Arrays.asList(shortcut));

另外上述代码中list是获取的快捷方式列表。

 List<ShortcutInfo> list= shortcutManager.getDynamicShortcuts();

好了,我们看下效果。

如图所示,点了五次后添加了五个快捷方式(最多展示四个),不过当我们点击第六次后,报错了。。

 java.lang.IllegalArgumentException: Max number of dynamic shortcuts exceeded
        at android.os.Parcel.readException(Parcel.java:1958)
        at android.os.Parcel.readException(Parcel.java:1900)
        at android.content.pm.IShortcutService$Stub$Proxy.addDynamicShortcuts(IShortcutService.java:493)

错误原因是已经超出最大动态快捷方式数,直接抛出了异常。

看下代码:

  public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
        try {
            return mService.setDynamicShortcuts(mContext.getPackageName(),
                    new ParceledListSlice(shortcutInfoList), injectMyUserId());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

好吧,那我们只能再捕获下异常了。

   try{
                if (shortcutManager.setDynamicShortcuts(list)){
                    Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e){
                Toast.makeText(this,"已达到最大快捷方式数",Toast.LENGTH_SHORT).show();
            }

解决。

接着我们看下update方法。

updateShortcuts与set或者add类似,都是传入的list。

与add不同的是,,传的shortcutInfo即使不存在,返回值也是true,但是并不会执行添加操作。

删除方法也很简单。

removeAllDynamicShortcuts:删除所有的快捷方式

另一种:removeDynamicShortcuts(@NonNull List<String> shortcutIds)。

需要注意的是:这些方法都是直接throw的异常,我们最好也try下。。

1.3固定快捷方式

在Android 8.0(API级别26)及更高版本上,可以创建固定快捷方式。

这时创建shortcut的方式有两种,一种是没有需要新建的,需要设置shortLabel等属性,另一种已存在的快捷方式,则只需要指定id就行。如:

  ShortcutInfo pinShortcutInfo =
                            new ShortcutInfo.Builder(this, "create0").build();

另外我们需要创建一个PendingIntent的对象,这个对象会在快捷方式成功固定时通知应用。

PendingIntent successCallback = PendingIntent.getBroadcast(this, /* request code */ 0,
        pinnedShortcutCallbackIntent, /* flags */ 0);
这里的intent需要shortcutManager来创建
  Intent pinnedShortcutCallbackIntent =
                            shortcutManager.createShortcutResultIntent(pinShortcutInfo);

最后 执行

 shortcutManager.requestPinShortcut(pinShortcutInfo,
                            successCallback.getIntentSender());

看下效果,当我们点击创建固定后会首先弹出提示框:

点击添加后,创建成功后的提示如下:

如果已存在该快捷方式:

 

如上图所示,桌面出现了一个个人主页0的快捷方式入口。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值