将菜单项添加到应用程序外部的弹出菜单 我想你正在寻找这个

问题描述

正如您在图片中看到的,当您单击应用程序时会出现一个弹出菜单认情况下,此菜单有 2 个项目(应用信息和暂停应用)。如何向此弹出菜单添加额外的菜单项?

enter image description here

解决方法

我想你正在寻找这个

https://developer.android.com/guide/topics/ui/shortcuts

AndriodManifest.xml

<meta-data android:name="android.app.shortcuts"
       android:resource="@xml/shortcuts" />

创建一个新的资源文件:res/xml/shortcuts.xml

在文件中定义快捷方式

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
 <shortcut
 android:shortcutId="compose"
 android:enabled="true"
 android:icon="@drawable/compose_icon"
 android:shortcutShortLabel="@string/compose_shortcut_short_label1"
 android:shortcutLongLabel="@string/compose_shortcut_long_label1"
 android:shortcutDisabledMessage="@string/compose_disabled_message1">
  <intent
   android:action="android.intent.action.VIEW"
   android:targetPackage="com.example.myapplication"
   android:targetClass="com.example.myapplication.ComposeActivity" />
   <!-- If your shortcut is associated with multiple intents,include them
     here. The last intent in the list determines what the user sees when
     they launch this shortcut. -->
   <categories android:name="android.shortcut.conversation" />
 </shortcut>
<!-- Specify more shortcuts here. -->
</shortcuts>

更多参考:https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts

,

这些在 Android 中称为应用快捷方式。

为此,您必须创建一个名为 shortcuts.xml 的 xml 并在 AndroidManifest.xml 中做一些更改

在文件夹 shortcuts.xml 中创建一个名为 res/xml-v25/ 的文件。这是因为在 v25 以上支持快捷方式

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

  <shortcut
    android:enabled="true"
    android:icon="@drawable/your_shortcut_icon"
    android:shortcutId="custom_shortcut_id"
    android:shortcutLongLabel="@string/shortcut_long_label"
    android:shortcutShortLabel="@string/shortcut_short_label">
    
    // Associate action with it.
    <intent
      android:action="android.intent.action.VIEW"
      android:targetClass="com.package.MainActivity"
      android:targetPackage="com.package"/>

  </shortcut>
</shortcuts>

并在应用了 MAIN 和 LAUNCHER 意图过滤器的活动下的 AndroidManifest.xml 中。

...
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>


        // Add this meta-data    
        <meta-data
            android:name="android.app.shortcuts"
            android:resource="@xml/shortcuts"/>
    </activity>
...

详情请参考这里