Search code examples
google-glassgoogle-gdk

Card not showing; goes straight to home card


I am trying to show a card so I know everything up to that point works. However, when I try to display the card, it just goes straight to the home card. The card I was trying to show was just going to display what was said in the voice recognizer before but that didn't work so I just put plain text and that didn't work either. Application goes - voice trigger --> voice recognizer --> this service:

public class MedMinderService extends Service {
public String MedName;
public String voiceResults;
private static final String TAG = "ShowData";
private static final String LIVE_CARD_ID = "showdata";
public static final String PREFS_NAME = "MyPreferencesFile";
private TimelineManager mTimelineManager;
private LiveCard mLiveCard;


@Override
public void onCreate() {
    super.onCreate();
    mTimelineManager = TimelineManager.from(this);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

public int onStartCommand(Intent intent, int flags, int startId) {

    String voiceResults = intent.getExtras()
            .getString(RecognizerIntent.EXTRA_RESULTS);

    String MedName = voiceResults; //MedName declared

    SharedPreferences MedInfo = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = MedInfo.edit();
    editor.putString("MedName", MedName.toString());

    editor.commit();

    mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);

    Intent i = new Intent(this, ShowDataActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    return START_STICKY;
    }

}

The intent at the bottom goes to this activity:

public class ShowDataActivity extends Activity {
private LiveCard mLiveCard;
public static final String PREFS_NAME = "MyPreferencesFile";
private GestureDetector mGestureDetector;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SharedPreferences MedInfo = getSharedPreferences(PREFS_NAME, 0);

Card ShowDataCard = new Card(this);
ShowDataCard.setText("IT WORKS!");
//ShowDataCard.setText(MedInfo.getString("MedName", "your medication"));
View ShowDataCardView = ShowDataCard.toView();
setContentView(ShowDataCardView);
}

The "ShowDataCard" that has been commented out is what I was origonally trying to do with the voice recognition but it wouldn't even work with the text "IT WORKS!"

Again: I am just trying to show a card with the text "IT WORKS"

thanks


Solution

  • The easiest way to get a live card to appear with just text is using widgets that are compatible with RemoteViews. You can find a list of them in the GDK documentation here:

    https://developers.google.com/glass/develop/gdk/ui/live-cards

    under the Creating low-frequency live cards section.

    Here is some sample code (based on your code above) that can get that working quickly:

    final String LIVE_CARD_ID = "showdata";
    
    mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);
    
    RemoteViews remoteViews = 
        new RemoteViews(getPackageName(), R.layout.layout_helloglass);
    
    mLiveCard.setViews(remoteViews);
    
    // Make sure Glass navigates to LiveCard immediately
    mLiveCard.setNonSilent(true); 
    
    mLiveCard.publish();
    

    The layout file can look like this for layout_helloglass.xml:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Hello, Glass!" />
    </FrameLayout>
    

    If you still want to navigate to another Activity from your LiveCard, you need to associate the Activity with a PendingIntent and then associate that PendingIntent with the LiveCard's action. This would happen immediately before the LiveCard.publish() method:

    Intent i = new Intent(this, ShowCardActivity.class); 
    mLiveCard.setAction(PendingIntent.getActivity(this, 0, i, 0));
    

    That should get you up and running! Hopefully this will help.