[英]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.