如何在启动器图标中添加像活动快捷方式这样的像素? [英] How to add pixel like activity shortcuts in launcher icon?

查看:79
本文介绍了如何在启动器图标中添加像活动快捷方式这样的像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since the release of pixel series, there has been this feature to add activity shortcuts in application icon itself by long pressing the icon. I have been trying to find out a particular method by which these shortcuts can be implemented to make an app more interactive and user friendly.

推荐答案

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

How to add pixel like activity shortcuts in launcher icon?

此选项可从Android的Oreo版本中获得

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

follow this steps to create activity shortcuts in launcher icon

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

  • In your app's manifest file (AndroidManifest.xml), find an activity whose intent filters are set to the android.intent.action.MAIN action and the android.intent.category.LAUNCHER category.

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

Add a <meta-data> element to this activity that references the resource file where the app's shortcuts are defined:

 <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.
  • Create a new resource file: res/xml/shortcuts.xml.

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

In this new resource file, add a root element, which contains a list of elements. Each element, in turn, contains information about a static shortcut, including its icon, its description labels, and the intents that it launches within the app:

<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>

此快捷方式的输出

有关更多信息,请阅读 应用程序快捷方式

for more information please read App Shortcuts

这篇关于如何在启动器图标中添加像活动快捷方式这样的像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆