Calvin
Calvin

Reputation: 652

Google Glass: Displaying a published LiveCard

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!

Upvotes: 0

Views: 970

Answers (3)

Tony Allevato
Tony Allevato

Reputation: 6429

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.

Upvotes: 2

mutexkid
mutexkid

Reputation: 1084

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.

Upvotes: 0

Harry Y
Harry Y

Reputation: 11

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.

Upvotes: 1

Related Questions