parohy
parohy

Reputation: 2180

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 ?

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

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.

Upvotes: 2

Views: 4943

Answers (2)

Catluc
Catluc

Reputation: 1823

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

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

Then in onCreate() put this:

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

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

Upvotes: 4

Štěp&#225;n
Štěp&#225;n

Reputation: 348

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):

...
<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:

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

Upvotes: 4

Related Questions