-1

I'm trying to create from my player app an home screen shortcut that will trigger the playback of a specific live stream, without opening an activity. Unfortunately this doesn't work...

ShortcutManager sM = c.getSystemService(ShortcutManager.class);

    Intent intent2 = new Intent(c.getApplicationContext(), APPBroadcastReceiver.class);
    intent2.setAction("toggle_intent");
    intent2.putExtra("id", 1);
    ShortcutInfo  shortcut2 = new ShortcutInfo.Builder(c,MSG_SHORCUT_CUSTOM)
            .setIntent(intent2)
            .setShortLabel("ShortLabel")
            .setLongLabel("LongLaber")
            .setDisabledMessage("DisabledMessage")
            .setIcon(Icon.createWithResource(c, R.mipmap.ic_add_outline_short))
            .build();
    listshortcut.add(shortcut2);

    Intent pinnedShortcutCallbackIntent = mShortcutManager.createShortcutResultIntent(shortcut2);
    PendingIntent successCallback = PendingIntent.getBroadcast(context, 0,  pinnedShortcutCallbackIntent, 0);

mShortcutManager.requestPinShortcut(pinShortcutInfo, successCallback.getIntentSender());

The shortcurt is created but all I get when I press on it is 'Application not installed' As soon as I replace my Receiver class by an activity the activity opens.

Is it possible to trigger a custom intent that won't open an activity from an home screen shortcut?

user1026605
  • 1,633
  • 4
  • 22
  • 58

1 Answers1

2

Not directly. As far as I know, app shortcuts can only point to Activities. But you can make a very thin Activity that just forwards Intents to your BroadcastReceiver:

public class IntentForwardActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent broadcastIntent = new Intent(getIntent());
        broadcastIntent.setClassName(getApplicationContext(), APPBroadcastReceiver.class);
        sendBroadcast(broadcastIntent);
        finish();
    }
}

In particular, Activities don't need to have a layout or call setContentView().

Jack Meister
  • 247
  • 3
  • 6
  • Thanks for this workaround, unfortunately the app reopens showing its current activity. I'm trying to toggle playback from the homescreen without opening the app UI – user1026605 Dec 28 '17 at 01:17
  • That sounds like a problem involving task affinity. Try setting the [launch mode](https://developer.android.com/guide/topics/manifest/activity-element.html#lmode) of `IntentForwardActivity` to `"singleInstance"` in your manifest. – Jack Meister Dec 28 '17 at 01:29
  • Thanks, but the problem is that we can see a screen being opened then closed, which won't work for the users... – user1026605 Dec 28 '17 at 02:14
  • I see. In that case, you could additionally make `IntentForwardActivity` [transparent](https://stackoverflow.com/questions/2176922/how-do-i-create-a-transparent-activity-on-android) and [turn its window animations off](https://stackoverflow.com/questions/6972295/switching-activities-without-animation). I understand that this approach seems hacky, but I know for a fact that short-lived transparent activities can work for things like this. (And since I still think that app shortcuts have to point to activities, I think there's no other way to do what you want.) – Jack Meister Dec 28 '17 at 02:49