1

我正在尝试展示一张卡片,所以我知道到目前为止一切正常。但是,当我尝试显示该卡时,它只是直接进入家庭卡。我试图展示的卡片只是要显示之前在语音识别器中所说的内容,但这不起作用,所以我只输入纯文本,这也不起作用。应用程序运行 - 语音触发器 -> 语音识别器 -> 此服务:

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;
    }

}

底部的意图用于此活动:

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);
}

已被注释掉的“ShowDataCard”是我最初尝试对语音识别进行的操作,但它甚至不适用于文本“IT WORKS!”

再说一遍:我只是想展示一张带有“IT WORKS”文字的卡片

谢谢

4

2 回答 2

5

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.

于 2013-12-09T00:37:08.117 回答
1

There was a bug in the GDK Sneak Peek that prevented voice prompts from creating Services. If one inserted a Log.d() call in a Service's onStartCommand() override, they would discover that it were never called.

This bug has been fixed in the GDK Preview. This behavior should not appear again.

This question was rewritten after the GDK Preview launch to remove this outdated answer. Thanks to user Falcon for notifying me.

于 2013-12-15T19:51:52.517 回答