【问题标题】:How to add pixel like activity shortcuts in launcher icon?如何在启动器图标中添加类似像素的活动快捷方式?
【发布时间】:2018-04-30 21:15:44
【问题描述】:

自从像素系列发布以来,就有了通过长按图标在应用程序图标本身添加活动快捷方式的功能。我一直在尝试找出一种特定的方法来实现这些快捷方式,以使应用程序更具交互性和用户友好性。

【问题讨论】:

  • Oreo 版本的 Android SDK 提供此选项。检查奥利奥的文档

标签: java android android-layout android-studio


【解决方案1】:

如何在启动器图标中添加类似像素的活动快捷方式?

Oreo 版本的 android 提供此选项

按照此步骤在启动器图标中创建活动快捷方式

  • 在应用的清单文件 (AndroidManifest.xml) 中,找到其意图过滤器设置为 android.intent.action.MAIN 操作和 android.intent.category.LAUNCHER 类别的 Activity。

  • 向此活动添加一个<meta-data> 元素,该元素引用定义了应用快捷方式的资源文件:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapplication">
  <application ... >
     <activity
        android:name=".activity.TempActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <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>
  • 新建资源文件:res/xml/shortcuts.xml.

在这个新的资源文件中,添加一个根元素,其中包含一个元素列表。每个元素依次包含有关静态快捷方式的信息,包括其图标、描述标签以及它在应用中启动的意图:

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

    <shortcut
        android:enabled="true" // make sure shortcut is enabled true
        android:icon="@drawable/ic_check" // set icon here
        android:shortcutDisabledMessage="@string/collections" // message when shortcut is  disabled
        android:shortcutId="prem" // you need to give unique shortcutId
        android:shortcutLongLabel="@string/collections" // long lable for shortcut
        android:shortcutShortLabel="@string/collections">// short lable for shortcut
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.prem.demoapp.activity.ChatActivity"
            android:targetPackage="com.prem.demoapp" /> // you need to provide here your Activity name and target package name you application


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


    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_check"
        android:shortcutDisabledMessage="@string/app_name"
        android:shortcutId="compose"
        android:shortcutLongLabel="@string/app_name"
        android:shortcutShortLabel="@string/app_name">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.prem.demoapp.activity.AccountSettingActivity"
            android:targetPackage="com.prem.demoapp" />

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



</shortcuts>

这个快捷方式的输出

更多信息请阅读App Shortcuts

【讨论】:

  • 好一个。该代码易于理解且工作正常。
【解决方案2】:

如果您的应用程序面向版本 7.1+(API 级别 25+),您只能使用这些快捷方式。

这些快捷方式共有三种不同类型,取自文档:

静态快捷方式在打包到 APK 的资源文件中定义。因此,您必须等到更新整个应用程序 更改这些静态快捷方式的详细信息。

动态快捷方式在运行时使用 ShortcutManager 发布 API。在运行时,您的应用可以发布、更新和删除其 动态快捷方式。

固定快捷方式在运行时发布,也使用 快捷方式管理器 API。在运行时,您的应用可以尝试将 快捷方式,此时用户会收到一个确认对话框询问 他们允许固定快捷方式。固定的快捷方式出现在 仅当用户接受固定请求时才支持启动器。 (仅适用于 Android 8.0+)

这些快捷方式至少引用了应用内的一个意图。我不会在这里复制粘贴文档中的教程,您可以找到您需要了解的所有内容here

【讨论】:

    猜你喜欢
    • 2011-10-23
    • 2011-12-23
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2011-03-26
    相关资源
    最近更新 更多