安卓 O 版固定图标 - CREATE_SHORTCUT 意图过滤器

6
在Android O的新“固定快捷方式”功能文档中(链接),他们提到“您还可以创建一个专门的活动,帮助用户创建快捷方式,包括自定义选项和确认按钮”。
我尝试遵循文档,但当我尝试创建新的快捷方式时,我只看到默认对话框,而不是我的活动。
以下是清单文件中的声明:
    <activity android:name=".ShortcutActivity">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        </intent-filter>
    </activity>

P.S
文档中还展示了Gmail应用的一个示例 - 我该如何进入那个界面?我想查看流程,但是找不到它。


你是否已经包含了元数据? - Amrutha Saj
@OriWasserman,请查看我的更新答案。 - Mightian
4个回答

3

您需要创建一个新的对话框活动,然后可以在该对话框上添加任何选项,并根据您的意愿处理addShortcut方法。

我尝试使用该代码添加和删除动态快捷方式,它有效。

AndroidManifest.xml

<activity android:name=".DialogActivity"
        android:excludeFromRecents="true"
        android:theme="@style/Theme.AppCompat.Dialog"
        >
...
</activity>

DialogActivity.java

public class DialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        this.setFinishOnTouchOutside(false);
        findViewById(R.id.add_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addShortcut();
            }
        });
        findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    ...
}

activity_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dialog"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.metax.myapplication.DialogActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Do you want to add shortcut?"/>

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_alignParentStart="true"
        android:text="No thanks"/>
    <Button
        android:id="@+id/add_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_alignParentEnd="true"
        android:text="Add me"/>
</RelativeLayout>

1
在你的Java类中插入:

 ShortcutManager sM = c.getSystemService(ShortcutManager.class);

    Intent intent2 = new Intent(c.getApplicationContext(), ShortcutActivity.class);
    intent2.setAction(Intent.ACTION_VIEW);
    ShortcutInfo  shortcut2 = new ShortcutInfo.Builder(c,MSG_SHORCUT_CUSTOM)
            .setIntent(intent2)
            .setShortLabel("ShortLabel")
            .setLongLabel("LongLaber")
            .setDisabledMessage("DisabledMessage")
            .setIcon(Icon.createWithResource(c, R.mipmap.ic_add_outline_short))
            .build();
    listshortcut.add(shortcut2);

    Intent pinnedShortcutCallbackIntent = mShortcutManager.createShortcutResultIntent(shortcut2);
    PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,  pinnedShortcutCallbackIntent, 0);

mShortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());
    sM.setDynamicShortcuts(listshortcut);

为什么需要调用createShortcutResultIntent?为什么不直接使用null作为第二个参数调用requestPinShortcut?采用这种方式有什么好处吗? - android developer

0

要使用ACTION_CREATE_SHORTCUT作为操作来启动您的快捷方式活动,请长按应用程序图标,然后按小部件图标,您会注意到一个1x1的小部件,在点击它时,您想要的活动将被启动。

此外,您也可以在某些所需的操作上从您的应用程序显式地触发该意图。


-3

创建新的资源文件: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="your package"
          android:targetClass="com.example.myapplication.ComposeActivity" />
         </shortcut>
      <!-- Specify more shortcuts here. -->
    </shortcuts>

manifest 中的 activity 标签中添加以下内容,
<meta-data android:name="android.app.shortcuts"
                 android:resource="@xml/shortcuts" />

如果您想了解更多,请参考doc,其中详细介绍了固定快捷方式、静态和动态快捷方式。

这里有一个来自谷歌的example,在他们的示例存储库中。


这不仅适用于静态和动态快捷方式吗?我在问固定的快捷方式。我的问题不在于创建快捷方式,而是他们提到的自定义活动。 - Ori Wasserman
@OriWasserman,你有办法自定义固定快捷方式对话框吗?我总是看到“添加到主屏幕”标题和一些带有1x1图标的文本。 - Akshat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接