【问题标题】:Android O Pinned Shortcuts - CREATE_SHORTCUT intent filterAndroid O 固定快捷方式 - CREATE_SHORTCUT 意图过滤器
【发布时间】:2017-10-10 08:38:56
【问题描述】:

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

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

这是 Manifest 中的声明:

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

附言
在文档中,他们还显示了 Gmail 应用程序中的示例 - 我如何进入该屏幕?我想看看流程,但找不到。

【问题讨论】:

  • 你是否包含了元数据
  • @OriWasserman 查看我的更新答案

标签: java android notifications android-8.0-oreo


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在你的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?为什么不直接调用requestPinShortcut,第二个参数为null?以这种方式拥有它为您提供了什么?
    【解决方案3】:

    要使用 ACTION_CREATE_SHORTCUT 启动快捷方式活动,长按应用程序图标,然后按下小部件图标,您会注意到一个 1x1 小部件,点击该小部件即可启动所需的活动。

    您还可以在应用中明确触发该意图,以及执行某些所需的操作。

    【讨论】:

      【解决方案4】:

      创建一个新的资源文件: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>
      

      ma​​nifest 中将其添加到活动标记中,

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

      如果您想了解更多信息,请参阅doc,它解释了关于固定快捷方式、静态和动态快捷方式的信息。

      这是来自谷歌的example,在他们的示例仓库中

      【讨论】:

      • 这不只适用于静态和动态快捷方式吗?我在问固定的快捷方式。我的问题不在于创建快捷方式,而在于他们提到的自定义活动
      • @OriWasserman 你有办法自定义固定快捷方式对话框吗?我总是看到“添加到主屏幕”标题和一些带有 1x1 图标的文本。
      猜你喜欢
      • 2019-04-12
      • 2019-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多