如何向应用程序快捷方式意图中添加数据 [英] How to add data to app shortcut Intent

查看:120
本文介绍了如何向应用程序快捷方式意图中添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是将某个键值传递给意图,以指示应用程序已通过快捷方式打开,因此它执行某些任务而不是标准启动.就我而言,该应用程序需要先从服务器下载数据,然后才能继续.我希望该快捷方式在安装后可用,所以我不能像启动后那样放置动态快捷方式.我还尝试通过打开一个特殊的活动来实现此目的,在该活动中我将键放在意图上,但是我需要针对每个快捷方式分别执行此操作,因为我不知道如何确定用户点击哪个快捷方式.我如何才能将原始数据放入Shortcut.xml的意图中?

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 ?

EDIT_1 :这是低谷类别吗?也许是一种方法.

EDIT_1: Would that be trough categories? Maybe one way of doing it.

EDIT_2当前解决方案:我通过将类别放在xml文件的快捷方式中解决了该问题.我仍然如何寻找将原始数据放入xml intent中的名称空间.

EDIT_2 current solution: I solved it by putting category in the shortcut intent in the xml file. How ever I am still looking for a namespace for putting primitive data into xml intent.

推荐答案

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

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

  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 shorcut>意向下使用此操作:

For Static Shorcut if you want to choose the action based on the shortcut you pressed use this under your xml shorcut > intent :

<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";

然后在onCreate()中输入以下内容:

Then in onCreate() put this:

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

Android中的

P.D WTF是多么可怕的失败,不要认为不好!:D

P.D WTF in Android is What a Terrible Failure, don't think bad! :D

这篇关于如何向应用程序快捷方式意图中添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆