簡體   English   中英

Google glass:有沒有一種方法可以從UI服務開始語音識別?

[英]Google glass: Is there a way to start speech recognition from a UI service?

我有一個使用LiveCards的玻璃應用程序。 因此,我沒有明確的運行活動,而只是與LiveCard交互以獲取信息的后台服務。 在某些時候,我想提出語音輸入。 問題在於代碼示例告訴您使用startActivityForResult ,而我無法在服務中執行此操作。 所以-有其他方法可以提起此問題,還是我不能在當前配置中做到這一點?

我也遇到了這個問題,不是語音輸入,而是我需要運行一個活動來使信息顯示在“低頻渲染”實時卡中,而無需用戶先打開菜單。 我認為您可以使用一項活動來獲取文本輸入,然后將其發送回服務。

有關如何綁定服務的大多數信息來自http://developer.android.com/guide/components/bound-services.html

MainService這是由“確定的玻璃,...”啟動的服務。布局只有一個帶有id文本的TextView。

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

}

TestActivity它在MainService延遲后運行,無需用戶輸入即可自動更新實時卡上的文本。

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

}

MenuActivity這是設置為活動卡的掛起意圖的活動。 當觸摸板被點擊時,它會彈出一個菜單以退出或更新文本。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM