java - Android O 固定快捷方式 - CREATE_SHORTCUT Intent 过滤器

标签 java android notifications android-8.0-oreo

In the documentation对于 Android O 中新的“固定快捷方式”功能,他们提到“您还可以创建一个专门的 Activity 来帮助用户创建快捷方式,完成自定义选项和确认按钮”。

我尝试按照文档进行操作,但是当我尝试创建一个新的快捷方式时,我只看到了默认对话框,而不是我的 Activity 。

这是 list 中的声明:

    <activity android:name=".ShortcutActivity">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT"/>
        </intent-filter>
    </activity>

附言
在文档中,他们还在 Gmail 应用程序中展示了一个示例——我如何进入该屏幕?想看流量但是没找到。

最佳答案

您必须创建一个新的 Activity 作为对话框,然后您可以在该对话框上添加您想要的任何选项,并根据需要使用 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>

关于java - Android O 固定快捷方式 - CREATE_SHORTCUT Intent 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662186/

相关文章:

ipad - 意外的 UIDeviceOrientationDidChangeNotification 被调用

java - Fargate 推出的容器的自省(introspection)

java - 如何使用 swing 找出窗口位于桌面上的位置

android - Flutter 的 TextPainter 抛出 '!_needsLayout' : is not true. ' 异常

java - 如何通过 Java w3c dom api 获取 xml 元素所在的行号

internet-explorer - IE 中的 Webkit 桌面通知

notifications - 从仪表板启动应用程序时的 iOS 5 远程通知

Java setter、getter(掷骰子)

java - 如何更改 JTextArea 中光标的默认位置?

android - 如何在 Android Jetpack Compose 中创建任意方向的拖动手势