繁体   English   中英

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

[英]How to add pixel like activity shortcuts in launcher icon?

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

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

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

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

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

  • 向此活动添加<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>

该快捷方式的输出

在此处输入图片说明

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

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

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

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

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

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

这些快捷方式引用了应用程序内的至少一个意图。 我不会复制粘贴在这里从文档的教程,你可以找到你所需要知道的一切在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM