Le framework natif Android AppShortcuts utilisé. Bouton de raccourci du bureau
Il existe deux façons d'implémenter la première de manière statique de configuration manifeste.
Utilisation: Ajoutez le méta-adta suivant à la classe de démarrage
<activity android:name=".MainActivity">
<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>
Créez un fichier de raccourcis dans le dossier xml :
Chaque raccourci est un élément de la boîte contextuelle. Jusqu'à 4 peuvent être ajoutés (Google dit 5)
icône: icône d'élément
shortcutId: l'ID de l'élément ne peut pas être répété. S'il est répété, seul le premier sera affiché.
shortcutLongLabel: texte de l'élément
intention: cliquez sur l'intention cible
targetClass: chemin de classe cible
targetPackage: nom du package d'application cible (il se peut qu'il ne s'agisse pas de votre propre application, tant que vous pouvez trouver la classe correspondante après l'installation, vous pouvez sauter)
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:enabled="true"
android:icon="@mipmap/del"
android:shortcutDisabledMessage="@string/static_message"
android:shortcutId="static1"
android:shortcutLongLabel="@string/static_long_label_1"
android:shortcutShortLabel="@string/static_short_label_1">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.myapplication.StaticTestActivity"
android:targetPackage="com.example.myapplication" />
</shortcut>
<shortcut
android:enabled="true"
android:icon="@mipmap/close"
android:shortcutDisabledMessage="@string/static_message"
android:shortcutId="static2"
android:shortcutLongLabel="@string/static_long_label_2"
android:shortcutShortLabel="@string/static_short_label_2">
<intent
android:action="android.intent.action.VIEW"
android:targetClass="com.example.myapplication.StaticTestActivity"
android:targetPackage="com.example.myapplication" />
</shortcut>
</shortcuts>
Besoin de définir le contenu d'affichage sur le fichier string.xml. Sinon, une erreur est signalée: la résolution d'attribut a échoué
<string name="static_message">asdf</string>
<string name="static_long_label_1">qwe</string>
<string name="static_short_label_1">qwe1</string>
<string name="static_long_label_2">qwe2</string>
<string name="static_short_label_2">qwe3</string>
Le second est configuré dynamiquement dans le code.