Is it possible to using voice commands for navigat

2019-08-05 18:24发布

问题:

Hi, there

Currently, I'm develop an immersion app to provide a text on screen and user can swipe_right to go another one.

Actually, It adapt from sample immersion pattern called charades(google development site).

My objective is, I want to using voice commands, instead of SWIPE gesture.

for example;

  1. User open the immersion demo, the screen will show first TEXT.
  2. User want to go next TEXT by using voice "GO NEXT".
  3. The screen will show another Text.

Considering this post!

Is there any way to do this? or any suggestion?

回答1:

Here, It is my solution. Hope this might helped someone who looking for.

I used Contextual voice commands to provide 'Next', 'Save' and 'Exit' commands for an user. you can go to this document from google dev site to see the ideas of doing this.

I have my layout activity to show some TEXT, so I put this code structure. in my layout activity

    //contextual voice command
import com.google.android.glass.view.WindowUtils;
import android.view.Menu;
import android.view.MenuItem;


@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    // Pass through to super to setup touch menu.
    return super.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
        switch (item.getItemId()) {

            case R.id.save_menu_item:
                Log.d("Contextual", "go save checks");  
                break;
            case R.id.next_menu_item:
                Log.d("Contextual", "go next checks");                    
                break;
            case R.id.exit_menu_item:
                Log.d("Contextual", "go exit checks");                    
                break;
            default:
                return true;
        }

        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}                                                                                                                

Don't forget to declare this line getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS); to your onCreate(); before your setContentView().

Next thing, I created 'menu folder' and main.xml inside its to provide my item selection. Like this

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/next_menu_item"
    android:title="@string/next">

</item>
<item
    android:id="@+id/save_menu_item"
    android:title="@string/save_this">
</item>
<item
    android:id="@+id/exit_menu_item"
    android:title="@string/exit">
</item>

and my strings.xml file.

<resources>
   <string name="next">next</string>
   <string name="save_this">save</string>
   <string name="exit">exit</string>
</resources>

put this line <uses-permission android:name="com.google.android.glass.permission.DEVELOPMENT" />

to your AndroidMenifest.xml.

and it works fine for me !