0

I'm having trouble displaying a LiveCard.

public class RollTheDiceActivity extends Activity {
    private LiveCard mLiveCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_roll_the_dice);
        //                      ^^^^^^^^^^^^^^^^^^^^^^    
        publishCard(this);
    }

    private void publishCard(Context context) {
        // Already published
        if (mLiveCard != null)
            return;

        String cardId = "my_card";
        TimelineManager tm = TimelineManager.from(context);
        mLiveCard = tm.getLiveCard(cardId);

        RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(),
                R.layout.livecard_roll_the_dice);
        //               ^^^^^^^^^^^^^^^^^^^^^^
        mLiveCard.setViews(mRemoteViews);

        Intent intent = new Intent(context, RollTheDiceActivity.class);
        mLiveCard.setAction(PendingIntent.getActivity(context, 0, intent, 0));
        mLiveCard.publish();
    }
}

I expected to see the contents livecard_roll_the_dice instead of activity_roll_the_dice, since publishing will be instant and take over the screen.

Instead, activity_roll_the_dice content is showing. I think this means that the mLiveCard is either never published or published but not pushed to the screen.

How do I show the contents of a published card on the screen?

In case it helps, I'm launching the app through a voice trigger from home screen: "Ok Google, roll the dice"

Thank you!

Calvin
  • 622
  • 2
  • 8
  • 19

3 Answers3

2

Live cards are published in the background unless you pass PublishMode.REVEAL to the publish method to force it to be displayed. However, a larger problem is that live cards should be published by a background service rather than an activity. Live cards need to be owned by a long-running context in order to stay alive in the timeline while the user is navigating elsewhere in the timeline or in immersions.

So, if you want an activity to also publish a live card, you should put the live card code in a service and have the activity make a call to that service (e.g., using a binder) to publish the card.

Is there a reason you're using an activity at all and setting its content view when you expect the live card to be displayed immediately? If that's the behavior you want, you might consider removing the activity entirely and using a service instead, with the voice trigger attached to the service. The compass sample provides an example of this.

Tony Allevato
  • 6,429
  • 1
  • 29
  • 34
  • Thank you Tony. Adding `setNonSilent(true)` made the `activity_roll_the_dice` and `livecard_roll_the_dice` flash one after another, then the activity was closed and returned to home screen. I didn't realize how short lived activities are. – Calvin Dec 14 '13 at 00:52
  • I have a follow up question... When I made a [sample app](https://developers.google.com/glass/develop/gdk/ui/theme-widgets#creating_glass-styled_cards) for showing puppy pictures, the cards stayed up until I closed app by swiping down. Why is it that the live cards didn't stay up like the puppy pictures? – Calvin Dec 14 '13 at 00:58
  • Activities will live at least as long as the user is viewing and interacting with it, or until you do something to navigate the user away from it (the details are a bit finer than this, since an activity might stay alive in the background after the user has left it). In this case, by publishing the live card with `setNonSilent(true)`, you were asking the system to immediately navigate to the card, leaving the activity in the process. – Tony Allevato Dec 14 '13 at 00:58
  • Let me answer my own second question... The live card was closed immediately because I was unpublishing the cards from `onDestroy()` of the activity. As you mentioned, publishing the live card destroyed the activity - this unpublished the live card and closed it. – Calvin Dec 14 '13 at 01:13
  • Note that today setNonSilent() doesn't exist anymore. Use publish(LiveCard.PublishMode.REVEAL) instead. – patapizza Mar 13 '14 at 19:38
1

Calvin, the live card's "life" should be tied to something more "persistent", as the previous poster points out. If you look at my example code, it always uses a background service to control the life of the live card. Your activity will come and go (paused/resumed) whereas the live card is "pinned" and it's always there until you explicitly "unpublish" it.

Harry Y
  • 11
  • 1
0

One other thing i found that might save someone a bit of time with this problem!

When using RemoteViews for "low frequency updates" from a service to manage a LiveCard (rather than a DirectRenderingCallback for "high frequency" updates), ensure you DONT call setDirectRenderingEnabled(true) on the LiveCard.

This will cause the RemoteView to not work at all! Removing the setDirectRenderingEnabled from the liveCard when using a RemoteView to manage its view resource fixed the livecard not appearing issue for me.

mutexkid
  • 1,076
  • 1
  • 12
  • 12