简体   繁体   English

如何将数据添加到应用程序快捷方式 Intent

[英]How to add data to app shortcut Intent

What I want to do is to pass a certain key value to the intent indicate the app that it was open trough shortcut, so it does certain task instead of standard launch.我想要做的是将某个键值传递给意图指示应用程序它是通过快捷方式打开的,因此它执行某些任务而不是标准启动。 In my case the app needs to download data from the server before it continues.在我的情况下,应用程序需要在继续之前从服务器下载数据。 I want the shortcut to be available after install so I cannot put a dynamic shortcut as I did so far after launch.我希望快捷方式在安装后可用,所以我不能像启动后那样放置动态快捷方式。 I also tried to do this by opening a special activity where I put the key to the intent, but I would need to do this for every shortcut separately because I don't know ho to determine which shortcut the user tap.我还尝试通过打开一个特殊的活动来做到这一点,我将关键的意图放在其中,但我需要分别为每个快捷方式执行此操作,因为我不知道如何确定用户点击哪个快捷方式。 How can I put primitive data to the intent in the shortcut.xml ?如何将原始数据放入shortcut.xml 中的意图?

EDIT_1: Would that be trough categories? EDIT_1:那会是低谷类别吗? Maybe one way of doing it.也许是一种方法。

EDIT_2 current solution: I solved it by putting category in the shortcut intent in the xml file. EDIT_2 当前解决方案:我通过在 xml 文件的快捷方式意图中放置类别来解决它。 How ever I am still looking for a namespace for putting primitive data into xml intent.我一直在寻找一个命名空间来将原始数据放入 xml 意图中。

You should probably check android documentation and have a look to how to add a Dynamic Shortcut check this out, you can pass a intent this way, using a Bundle perhaps Dynamic Shortcut App您可能应该查看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));

For Static Shorcut if you want to choose the action based on the shortcut you pressed use this under your xml shorcut > intent :对于静态快捷方式,如果您想根据您按下的快捷方式选择操作,请在您的 xml 快捷方式 > 意图下使用它:

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

Then in your activity declare a constant.然后在您的活动中声明一个常量。

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

Then in onCreate() put this:然后在 onCreate() 中输入:

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

PD WTF in Android is What a Terrible Failure , don't think bad! Android 中的 PD WTF 是多么可怕的失败,别想坏了! :D :D

If you want to set the intent's data uri, you can do it by setting it in the static xml ( https://developer.android.com/guide/topics/ui/shortcuts.html#static ):如果要设置意图的数据 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" />
...

In your activity, you get the data uri back:在您的活动中,您将获得数据 uri:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM