美文网首页
AppShortcuts

AppShortcuts

作者: wmszhe | 来源:发表于2017-01-05 12:09 被阅读37次

最近看了appshortcuts的问题,整理了一下

1.静态配置AppShortcuts功能

动态更新和删除功能只能操作动态设置的AppShortcuts

  • 在App入口activity<intent-filter>标签下添加
<meta-data
  android:name="android.app.shortcuts"
  android:resource="@xml/shortcuts"/>
  • 创建shortcuts文件
<?xml version="1.0" encoding="utf-8"?>
<shrotcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutDisabledMessage="@string/shortcutDisabledMessage"
        android:shortcutId="shortcut1"
        android:shortcutLongLabel="@string/shortcutLongLabel"
        android:shortcutShortLabel="@string/shortcutShortLabel">
        <intent
            android:action="action1"
            android:targetClass="com.example.appshortcuts.Test1Activity"
            android:targetPackage="com.example.appshortcuts"/>
    </shortcut>
    
    <shortcut
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:shortcutDisabledMessage="@string/shortcutDisabledMessage2"
        android:shortcutId="shortcut2"
        android:shortcutLongLabel="@string/shortcutLongLabel2"
        android:shortcutShortLabel="@string/shortcutShortLabel2">
        <intent
            android:action="action2"
            android:targetClass="com.example.appshortcuts.MainActivity"
            android:targetPackage="com.example.appshortcuts"/>
        <intent
            android:action="action3"
            android:targetClass="com.example.appshortcuts.Test2Activity"
            android:targetPackage="com.example.appshortcuts"/>
    </shortcut>
</shrotcuts>

shortcut下只有一个intent时,按返回键返回桌面,可创建多个intent,实现按返回键时回到指定页面

enabled shortcut是否启用

shortcutDisabledMessage:当你禁用了shortcut之后,它将不会显示在用户长按应用图标后打开的快捷方式里,但是用户可以把一个快捷方式拖拽到launcher的某个页面,被禁用之后这个快捷方式就会显示为灰色,点击则会显示一个内容为shortcutDisabledMessage的Toast。------------这个配置是在我们选择一个不可用的shortcut时给用户的一个提示

shortcutLongLabel 当空间足够时,显示此标签下的内容

shortcutShortLabel 当空间不足时,显示此标签下的内容

2.动态配置AppShortcuts

private ShortcutManager mShortcutManager;
mShortcutManager = getSystemService(ShortcutManager.class);
  • 添加 setDynamicShortcuts addDynamicShortcuts
ShortcutInfo info = new ShortcutInfo.Builder(MainActivity.this, "shrotId11")
    .setShortLabel("Add Label")
    .setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher))
    .setIntent(new Intent(Intent.ACTION_VIEW, null, MainActivity.this, Test3Activity.class))
    .build();
mShortcutManager.setDynamicShortcuts(Arrays.asList(info));
  • 更新 updateShortcuts
ShortcutInfo info = new ShortcutInfo.Builder(MainActivity.this, "shrotId11")
        .setShortLabel("Update Label")
        .setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.ic_launcher))
        .setIntent(new Intent(Intent.ACTION_VIEW, null, MainActivity.this, Test3Activity.class))
        .build();
mShortcutManager.updateShortcuts(Arrays.asList(info));
  • 删除 removeDynamicShortcuts
mShortcutManager.removeDynamicShortcuts(Arrays.asList("shrotId11"));

3.Shortcuts的个数和次序

Shortcuts的总数不能超过5个, 即静态和动态shortcuts加起来总数最多是五个.

通过setRank()改变顺序 --不接受负值

4.Pinning Shortcuts

用户可以将AppShortcuts添加到桌面--开发者是没有权利去删除的, 能删除它的只有用户,但是可以disable,disable后变灰,点击时提示disableShortcuts第二个参数的信息

mShortcutManager.disableShortcuts(Arrays.asList(shortcutId), "暂时不可用");
mShortcutManager.removeDynamicShortcuts(shortcutId);

相关文章

  • AppShortcuts

    最近看了appshortcuts的问题,整理了一下 1.静态配置AppShortcuts功能 动态更新和删除功能只...

  • Android App Shortcuts学习总结

    一、简介 1、简述 我们可以在Android 7.1(API 25)中,使用AppShortcuts。当我们长按应...

网友评论

      本文标题:AppShortcuts

      本文链接:https://www.haomeiwen.com/subject/pbczvttx.html