Android 谷歌眼镜:有没有办法从用户界面服务开始语音识别?

Android 谷歌眼镜:有没有办法从用户界面服务开始语音识别?,android,google-glass,google-gdk,Android,Google Glass,Google Gdk,我有一个使用LiveCards的glass应用程序。因此,我没有显式地运行活动,只有一个与LiveCard交互以获取信息的后台服务。在某个时候,我想打开一个语音输入。问题是代码示例告诉您如何使用,这不是我在服务中可以做到的。那么-是否有其他方法来显示此信息,或者我可以在当前配置中不执行此操作?我也遇到了此问题,不是语音输入,但我需要运行一个活动,以便在用户不首先打开菜单的情况下,将信息显示在“低频渲染”实时卡中。我认为您可以使用活动获取文本输入,然后将其发送回服务 关于如何绑定服务的大部分信息来

我有一个使用LiveCards的glass应用程序。因此,我没有显式地运行活动,只有一个与LiveCard交互以获取信息的后台服务。在某个时候,我想打开一个语音输入。问题是代码示例告诉您如何使用,这不是我在服务中可以做到的。那么-是否有其他方法来显示此信息,或者我可以在当前配置中不执行此操作?

我也遇到了此问题,不是语音输入,但我需要运行一个活动,以便在用户不首先打开菜单的情况下,将信息显示在“低频渲染”实时卡中。我认为您可以使用活动获取文本输入,然后将其发送回服务

关于如何绑定服务的大部分信息来自

main服务 这是由“ok glass,…”启动的服务。布局只有一个id为文本的文本视图

public class MainService extends Service {

    private static final String LIVE_CARD_TAG = "my_card";

    private final IBinder mBinder = new LocalBinder();

    LiveCard mLiveCard;
    TimelineManager mTimelineManager;
    RemoteViews mViews;

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

    public class LocalBinder extends Binder {
        MainService getService() {
            return MainService.this;
        }
    }

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

    public int onStartCommand(Intent intent, int flags, int startId) {
        mLiveCard = mTimelineManager.createLiveCard(LIVE_CARD_TAG);
        mViews = new RemoteViews(this.getPackageName(),R.layout.activity_main);
        mLiveCard.setViews(mViews);
        Intent mIntent = new Intent(this, MenuActivity.class);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mLiveCard.setAction(PendingIntent.getActivity(this, 0, mIntent, 0));
        mLiveCard.publish(LiveCard.PublishMode.REVEAL);
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        // run the test activity after the initial text has displayed
                        Intent testIntent = new Intent(getBaseContext(), TestActivity.class);
                        testIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        getApplication().startActivity(testIntent);
                    }
                }, 3000);
        return START_STICKY;
    }

    public void updateText(String textString) {
        mViews.setTextViewText(R.id.text,textString);
        mLiveCard.setViews(mViews);
    }

    @Override
    public void onDestroy() {
        if (mLiveCard != null && mLiveCard.isPublished()) {
            Log.d("debug", "Unpublishing LiveCard");
            mLiveCard.unpublish();
            mLiveCard = null;
        }
        super.onDestroy();
    }

}
测试活动 这将在主服务延迟后运行,并自动更新实时卡上的文本,无需用户输入

public class TestActivity extends Activity {

    MainService mService;
    boolean mBound = false;

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

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, MainService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        // this crashes if run right away, so give it a little time
                        mService.updateText("Updated from TestActivity");
                        finish();
                    }
                }, 500);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };

}
菜单活动 这是设置为live卡的挂起意图的活动。点击触摸板时,会弹出一个菜单,退出或更新文本

public class MenuActivity extends Activity {

    MainService mService;
    boolean mBound = false;

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

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, MainService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        openOptionsMenu();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.stop:
                stopService(new Intent(this, MainService.class));
                return true;
            case R.id.update:
                mService.updateText("Updated from MenuActivity");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onOptionsMenuClosed(Menu menu) {
        finish();
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
        }
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
        }
    };
}

通常被接受的Android设计模式要求任何用户交互都应该涉及到用户(如果这是显而易见的,那么很抱歉)。这通常意味着使用用户可以与之交互的
活动
,或者至少在使用
服务时使用
通知
系统。考虑到安卓设备在给定的时间内可以做很多事情,我认为其中的一个应该是强制性的,以符合公认的设计模式。我理解并同意。问题是GDK引入了LiveCard的这种新模式,它是部分被动的,部分主动的。因此,对于如何正确地执行此操作,存在一些困惑。您能否描述一下您希望启动语音识别器的哪种交互?您想从与卡的操作相关联的挂起意图启动识别器还是其他什么?我想使用识别器作为排序提示。通过EXTRA_prompt传递一个提示,然后通知用户响应时所说的内容。挂起的意图对我不起作用,因为这不是对选择live card的响应,这是服务中发生的事情,我想让交互免提。为什么评级为-1。。。在一个被回答的,受欢迎的问题上?