【问题标题】:How to add data to app shortcut Intent如何将数据添加到应用快捷方式 Intent
【发布时间】:2017-03-02 11:34:12
【问题描述】:

我想要做的是将某个键值传递给意图,指示应用程序它是通过快捷方式打开的,因此它执行某些任务而不是标准启动。 在我的情况下,应用程序需要在继续之前从服务器下载数据。我希望快捷方式在安装后可用,因此我无法像启动后那样放置动态快捷方式。 我还尝试通过打开一个特殊的活动来做到这一点,在该活动中我将密钥放在意图中,但我需要为每个快捷方式分别执行此操作,因为我不知道如何确定用户点击哪个快捷方式。 如何将原始数据放入 shortcut.xml 中的意图?

EDIT_1:那会是低谷类别吗?也许是一种方法。

EDIT_2 当前解决方案:我通过将类别放入 xml 文件中的快捷方式意图来解决它。我怎么还在寻找将原始数据放入 xml 意图的命名空间。

【问题讨论】:

    标签: android android-manifest shortcut


    【解决方案1】:

    如果要设置intent的data uri,可以在静态xml中设置(https://developer.android.com/guide/topics/ui/shortcuts.html#static):

    ...
    <intent
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.yourname.yourapp"
        android:targetClass="com.yourname.yourapp.activities.ShortcutActivity"
        android:data="content://com.yourname.yourapp/compose/new?from=shortcut" />
    ...
    

    在您的活动中,您将获取数据 uri:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Uri data = this.getIntent().getData();
    }
    

    【讨论】:

      【解决方案2】:

      您可能应该查看 android 文档并查看如何添加动态快捷方式检查一下,您可以通过这种方式传递意图,使用 Bundle 可能是 Dynamic Shortcut App

        ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
      
          ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
              .setShortLabel("Web site")
              .setLongLabel("Open the web site")
              .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
              //check out this part
              .setIntent(new Intent(Intent.ACTION_VIEW,
                             Uri.parse("https://www.mysite.example.com/")))
              .build();
      
          shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
      

      对于静态快捷方式,如果您想根据您按下的快捷方式选择操作,请在您的 xml 快捷方式 > 意图下使用:

      <intent
       android:action="com.example.whatever.WTF"
       ..../>
      

      然后在你的活动中声明一个常量。

      private static final String ACTION_ADD_WTF ="com.example.whatever.WTF";
      

      然后在 onCreate() 中输入:

      if (ACTION_ADD_WTF.equals(getIntent().getAction())) {
                  // Invoked via the manifest shortcut.
                  //TODO:put your logic here
      }
      

      Android 中的 P.D WTF 是 多么可怕的失败,别想不好! :D

      【讨论】:

      • 是的,但是在您运行此代码后会添加动态快捷方式。我想在不需要启动应用程序的情况下有这个快捷方式。
      • 嗯,我从没想过动作,明天试试。
      猜你喜欢
      • 2019-07-14
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      相关资源
      最近更新 更多