1

I'm trying to create a pinned shortcut on the desktop for an app. The CreateShortcut method is called from a button and presents the android create-shortcut dialog. When the caller selects ok, the broadcast receiver should get called and execute finish so the activity exits.

This is the first time I've used a broadcast receiver but it looks like it's pretty straight-forward. Just create a receiver, register it with an intent filter that has the same action as an intent and when the intent is sent it should cause the receiver to be called, right?

The shortcut is created just fine but the broadcast receiver never gets called. I'm not seeing any messages on logcat.

private void CreateShortcut(final Context c) {
    if (ShortcutManagerCompat
             .isRequestPinShortcutSupported(c)) {

        Intent shortcutIntent = new Intent(
            c, CreateAppHomeShortcut.class);
        shortcutIntent.setAction(
            Intent.ACTION_CREATE_SHORTCUT);

        ShortcutInfoCompat shortcutInfo 
           = new ShortcutInfoCompat
            .Builder(c, "shortcut")
            .setShortLabel(c.getString(R.string.app_name))
            .setIcon(IconCompat.createWithResource(
                 c, R.drawable.qmark)
            )
            .setIntent(shortcutIntent)
            .build();

        registerReceiver(new BroadcastReceiver() {
                 @Override
                 public void onReceive(
                        Context context, Intent intent) {
                     Log.d(TAG, "msg received");
                     unregisterReceiver(this);
                     finish();
                 }
             }
            , new IntentFilter(
                 Intent.ACTION_CREATE_SHORTCUT
            )
        );
        PendingIntent successCallback = 
            PendingIntent.getBroadcast(
                c, 99
                , shortcutIntent, 0
           );
        ShortcutManagerCompat.requestPinShortcut(c,
             shortcutInfo,
             successCallback.getIntentSender()
        );
    }
}

I've been working on this several days and I'm stumped.
Thanks

1 Answer 1

2

I finally got the callback to my BroadcastReceiver. My main problem was that I was using the intents wrong. I thought that the brodcast receiver intent and the shortcut intent could be the same as long as the action was correct. Wrong! The shortcut intent must hava an action set but in the tests I did, it didn't seem to care what that action was. And the broadcast receiver was created as "Intent = new Intent(context, class); setAction(...); ", the shortcut would be created and function fine but the broadcast receiver was never invoked. The only way I could get the broadcast receiver to work was with an Intent just for it with nothing but the action set (or possibly extras) set. I couldn't get the program to work using the same intent to create the shortcut and invoke the broadcast receiver.

The other problem encountered was that the interface allows you to create multiple pinned shortcuts -- and would then call your broadcast receiver once for each shortcut created. I discovered you can query the interface for all pinned shortcuts and filter by id to find out if your shortcut already exists and use that info to avoid creating multiple identical pinned shortcuts on your home page.

The code below seems to work fine API26+ for creating a shortcut and the receiver gets called as long as the user accepts the shortcut. The docs state that they will only call your receiver on the user's acceptance. That of course makes detecting the end of the user's interaction rather difficult. Since the request gets buried in my actual app, the plan was to open this as part of a separate activity, but I don't have any way to detect that the user is done if he doesn't want the shortcut. If anyone has suggestions, I'd appreciate hearing them.

// Create a shortcut and exit the activity.  If the shortcut
   // already exists,just exit.
    private void CreateShortcut(final Context c) {
        if (Build.VERSION.SDK_INT >= 26) {
            ShortcutManager sm = 
                getSystemService(ShortcutManager.class);
            if (sm != null && sm.isRequestPinShortcutSupported()) {
                final String shortcutId = "StartApp";
                boolean shortcutExists = false;
                // We create the shortcut multiple times if given the
                // opportunity.  If the shortcut exists, put up
                // a toast message and exit.
                List<ShortcutInfo> shortcuts 
                     = sm.getPinnedShortcuts();
                for (int i = 0;
                      i < shortcuts.size() && !shortcutExists; i++) {
                    shortcutExists 
                       = shortcuts.get(i).getId().equals(shortcutId);
                if (shortcutExists) {
                    Toast.makeText(c , String.format(
                            "Shortcut %s already exists."
                            , shortcutId
                        )
                        , Toast.LENGTH_LONG
                    ).show();
                    finishActivity();
                }
                else {
                    // this is the intent that actually creates the
                    // shortcut.
                    Intent shortcutIntent
                        = new Intent(c, CreateAppHomeShortcut.class);
                    shortcutIntent.setAction(
                            Intent.ACTION_CREATE_SHORTCUT);
                    ShortcutInfo shortcutInfo = new ShortcutInfo
                        .Builder(c, shortcutId)
                        .setShortLabel(
                             c.getString(R.string.app_name))
                        .setIcon(createWithResource(c
                             , R.drawable.qmark))
                        .setIntent(shortcutIntent)
                        .build();
                    // this intent is used to wake up the broadcast
                    // receiver.
                    // I couldn't get createShortcutResultIntent to
                    // work but just a simple intent as used for a
                    // normal broadcast intent works fine.
                    Intent broadcastIntent
                        = new Intent(Intent.ACTION_CREATE_SHORTCUT);
                    // create an anonymous broadcaster.  Unregister
                    // to prevent leaks when done.
                    registerReceiver(new BroadcastReceiver() {
                             @Override
                             public void onReceive(
                                      Context c, Intent intent) {
                                 unregisterReceiver(this);
                                 Log.d(TAG, String.format(
                                     "ShortcutReceiver activity = "
                                        + "\"$1%s\""
                                         , intent.getAction()));
                                 finishActivity();
                             }
                         }
                        , new IntentFilter(
                              Intent.ACTION_CREATE_SHORTCUT)
                    );
                    PendingIntent successCallback 
                        = PendingIntent.getBroadcast(
                            c, 99
                            , broadcastIntent, 0);
                    // Shortcut gets created here.
                    sm.requestPinShortcut(shortcutInfo
                            , successCallback.getIntentSender());
                }
            }
        }
    }
1
  • 1
    bro u r awesome
    – Jeeva
    May 28, 2020 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.