Android App Shortcuts

Android App Shortcuts

说明

  1. Api起始版本25 (Android 7.1),要求min
  2. 每一个shortcuts都引用一个或者多个Intent,对应启动一个action或者task
  3. 快捷方式分为静态快捷方式(static Shortcuts)、动态快捷方式(Dynamic Shortcuts)、指针快捷方式(Pinned ShortCuts)
  4. 一个APP最大一次发布五个静态或者动态快捷方式,但是指针快捷方式不限个数。
  5. APP不能移除指针快捷方式,但是可以关闭指针快捷方式。
  6. Shortcut icons cannot include tints.
  7. 所有快捷方式信息都存储在凭证加密存储中,因此您的应用只有在解锁设备后才能访问用户的快捷方式。

快捷方式的显示顺序

  1. 静态快捷方式:isDeclaredInManifest()方法返回true的快捷方式。
  2. 动态快捷方式:isDynamic()方法返回true的快捷方式。

在每个快捷方式类型(静态和动态)中,根据getRank(),快捷方式按递增排序的顺序进行排序。

静态快捷方式

  1. 静态快捷方式不能有自定义意图标志。静态快捷方式的第一个意图将始终设置FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK。这意味着,当应用程序已经运行时,当启动静态快捷方式时,应用程序中的所有现有活动都将被销毁。注:如果这种行为不可取,您可以使用蹦床活动,或者在onCreate(Bundle)中启动另一个活动的不可见活动,然后调用finish():

在AndroidManifest.xml文件中,蹦床活动应包含属性分配android:taskAffinity =“”。
在快捷方式资源文件中,静态快捷方式中的意图应引用蹦床活动。

静态快捷方式的创建-Static Shortcuts

  1. 在清单文件中的启动activity中添加<meta-data> 元素,定义快捷方式指向的资源文件位置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapplication">
  <application ... >
    <activity android:name="Main">
      <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" />
    </activity>
  </application>
</manifest>
    
  1. 创建快捷方式的资源文件res/xml/shortcuts.xml:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
  <shortcut
    android:shortcutId="compose"
    android:enabled="true"
    android:icon="@drawable/compose_icon"
    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
    android:shortcutDisabledMessage="@string/compose_disabled_message1">
    <intent
      android:action="android.intent.action.VIEW"
      android:targetPackage="com.example.myapplication"
      android:targetClass="com.example.myapplication.ComposeActivity" />
    <!-- If your shortcut is associated with multiple intents, include them
         here. The last intent in the list determines what the user sees when
         they launch this shortcut. -->
    <categories android:name="android.shortcut.conversation" />
  </shortcut>
  <!-- Specify more shortcuts here. -->
</shortcuts>
    

注:android:targetClass指的是目标activity,android:targetPackage指的是目标APP的包名,也就是application id。这里经测试并不需要请求权限。

注:静态快捷方式必须提供的两个属性android:shortcutId and android:shortcutShortLabel

动态快捷方式(Dynamic Shortcuts)

动态创建快捷方式

1.动态快捷方式可以与任何intent flag一起发布,但是通常会指定FLAG_ACTIVITY_CLEAR_TASK,否则,如果应用程序已经在运行,应用程序将被简单地置于前台,并且目标活动可能不会出现。

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
    .setShortLabel("Web site")
    .setLongLabel("Open the web site")
    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
    .setIntent(new Intent(Intent.ACTION_VIEW,
                   Uri.parse("https://www.mysite.example.com/")))
    .build();

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


Pinned Shortcuts-固定快捷方式

  1. 需要用户确认才能添加快捷方式到设备屏幕上
  2. 用户还可以通过将应用程序的静态和动态快捷方式复制到启动器上来创建固定的快捷方式。

在Android O(Api26)及以上版本添加固定快捷方式

mShortCutId = "my-pinned-shortcuts";
        ShortcutInfo shortcutInfo;
        Intent mainIntent=new Intent(this,MainActivity.class);
        mainIntent.setAction(Intent.ACTION_MAIN);
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            shortcutInfo = new ShortcutInfo.Builder(this, mShortCutId)
                    .setIntent(mainIntent)
                    .setIcon(Icon.createWithResource(this,R.drawable.ic_person_gray_36dp))
                    .setShortLabel("my-shortcut-label1")
                    .build();
            ShortcutManager mShortcutManager =
                    this.getSystemService(ShortcutManager.class);
            if (mShortcutManager.isRequestPinShortcutSupported()) {
                Intent resultIntent = mShortcutManager.createShortcutResultIntent(shortcutInfo);
                PendingIntent successPendingIntent = PendingIntent.getBroadcast(this, 0, resultIntent, 0);
                mShortcutManager.requestPinShortcut(shortcutInfo, successPendingIntent.getIntentSender());
            }
        }

固定快捷方式会在以下三种情形下被移除

  1. 用户手动移除
  2. 和快捷方式关联的APP被卸载
  3. 通过系统设置APP,在关联的APP存储界面用户选择清除数据。

由于系统会自动执行固定快捷方式(Pinned Shortcut)的备份和恢复,因此这些快捷方式的ID应包含稳定的常量字符串或服务器端标识符,而不是其他设备上可能无意义的本地生成的标识符。

参考文档

  1. https://developer.android.com/reference/android/content/pm/ShortcutManager.html
  2. https://developer.android.com/guide/topics/ui/shortcuts.html
4311354-42c3ff6c5daf398a.jpg
扫码领红包支持

转载于:https://www.jianshu.com/p/96d73344bca5

猜你喜欢

转载自blog.csdn.net/weixin_33712987/article/details/91119547