2013-09-11 154 views
0

我想改變這個開關,以便不用點擊按鈕我的活動將通過說出相關水果的名稱啓動。例如,蘋果課程將通過講「蘋果」一詞來啓動。我應該如何重寫這個開關?我迄今爲止所做的所有嘗試似乎都沒有奏效。任何提供的答案將不勝感激。謝謝!如何使用語音識別器啓動我的Android活動?

package com.example.speech; 

import com.example.speech.R; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageButton; 

public class MainActivity extends Activity implements OnClickListener{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // find our buttons and set onClickListeners for all 
     ImageButton one = (ImageButton)findViewById(R.id.one); 
     ImageButton two = (ImageButton)findViewById(R.id.two); 
     ImageButton three = (ImageButton)findViewById(R.id.three); 
     ImageButton four = (ImageButton)findViewById(R.id.four); 
     ImageButton five = (ImageButton)findViewById(R.id.five); 
     ImageButton six = (ImageButton)findViewById(R.id.six); 
     one.setOnClickListener(this); 
     two.setOnClickListener(this); 
     three.setOnClickListener(this); 
     four.setOnClickListener(this); 
     five.setOnClickListener(this); 
     six.setOnClickListener(this); 
    } 

    public void onClick(View v) { 

     // do something based on the button that was clicked 
     switch(v.getId()) { 
      case R.id.one: 
       // create an intent indicating we want 
       // to start the activity. 

          Intent i = new Intent(this, Apple.class); 
       // start the activity based on the Intent 
       startActivity(i); 
       finish(); 
       break; 

      case R.id.two: 
        Intent j = new Intent(this, Orange.class); 

        // start the activity based on the Intent 
        startActivity(j); 
        finish(); 
        break; 

      case R.id.three: 
        Intent k = new Intent(this, Banana.class); 

        // start the activity based on the Intent 
        startActivity(k); 
        finish(); 
        break; 

      case R.id.four: 
        Intent l = new Intent(this, Grape.class); 

        // start the activity based on the Intent 
        startActivity(l); 
        finish(); 
        break; 

      case R.id.five: 
        Intent m = new Intent(this, Strawberry.class); 

        // start the activity based on the Intent 
        startActivityForResult(m, 0); 
        finish(); 
        break; 

      case R.id.six: 
        Intent n = new Intent(this, Kiwi.class); 

        // start the activity based on the Intent 
        startActivity(n); 
        finish(); 
        break; 
        default: 
        finish(); 
     } 

    }; 
} 

這是處理語音活動結果的正確方法嗎?

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == VOICE_RECOGNITION_REQUEST_CODE) 

      //If Voice recognition is successful then it returns RESULT_OK 
      if(resultCode == RESULT_OK) { 

       ArrayList<String> textMatchList = data 
       .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

       if (!textMatchList.isEmpty()) { 
        // If first Match contains the word 'apple' 
        // Then start the apple activity. 
        if (textMatchList.get(0).contains("apple")) { 
          // create an intent indicating we want 
       // to start the activity. 

          Intent i = new Intent(this, Apple.class); 
       // start the activity based on the Intent 
       startActivity(i); 
       finish(); 
       break; 

我覺得這個東西行後丟失......

if (textMatchList.get(0).contains("apple")) { 

回答

0

Here是關於如何使用Android的語音識別的樣本教程。您需要在OnActivityResult中處理搜索結果,並對蘋果,香蕉,葡萄等進行字符串匹配。如果找到字符串匹配,請啓動相應的活動!

+0

看看我的編輯。我需要在那裏添加什麼? – user2593697

+1

你做得對。使用RecognizerIntent啓動Activity(如示例代碼 - 檢查startVoiceRecognitionActivity())。一旦完成,它將調用onActivityResult和結果碼,然後檢查字符串匹配,然後開始相應水果的活動。 – prijupaul