2012-07-24 4 views
0

나는 절대적으로 효과가있는 음성 입력 클래스가 있습니다. 그러나 내 앱 사용자는 앱의 모든 페이지에서 음성 입력 코드를 사용할 수 있어야합니다. 내가해야할 일은 모든 xml에 버튼을 설정하여 모든 음성 입력 코드를 모든 단일 클래스에 복사하지 않고도 내 음성 입력 코드를 사용할 수있게하는 것입니다. 내 코드에서이 코드를 참조하면 코드에서 다른 클래스의 활동을이 버튼을 누르면 간단히 나타낼 수 있습니다. 코드는 다음과 같습니다. voiceinput code java class,이 코드를 사용하는 버튼을 가질 수있는 자바 클래스입니다.다른 클래스의 클래스 버튼 액션 참조

package com.example.com.proto1; 

import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.os.Bundle; 
import android.speech.RecognizerIntent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Sample code that invokes the speech recognition intent API. 
*/ 
public class VoiceRecognition extends Activity implements OnClickListener { 



    public static final int VOICE_RECOGNITION_REQUEST_CODE = 1234; 

    public ListView mList; 
    public Button speakButton; 

    /** 
    * Called with the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle voiceinput) { 
     super.onCreate(voiceinput); 

     // Inflate our UI from its XML layout description. 
     setContentView(R.layout.voice_recognition); 

     // Get display items for later interaction 

     voiceinputbuttons(); 


     // Check to see if a recognition activity is present 
     PackageManager pm = getPackageManager(); 
     List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
       RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); 
     if (activities.size() != 0) { 
      speakButton.setOnClickListener(this); 
     } else { 
      speakButton.setEnabled(false); 
      speakButton.setText("Recognizer not present"); 
     } 
    } 

    public void voiceinputbuttons() { 
     // TODO Auto-generated method stub 
     speakButton = (Button) findViewById(R.id.btn_speak); 
     mList = (ListView) findViewById(R.id.list); 
    } 

    /** 
    * Handle the click on the start recognition button. 
    */ 
    public void onClick(View v) { 
     if (v.getId() == R.id.btn_speak) { 
      startVoiceRecognitionActivity(); 
     } 
    } 

    /** 
    * Fire an intent to start the speech recognition activity. 
    */ 
    public void startVoiceRecognitionActivity() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 
       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 
       "Speech recognition demo"); 
     startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE); 
    } 

    /** 
    * Handle the results from the recognition activity. 
    */ 
    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == VOICE_RECOGNITION_REQUEST_CODE 
       && resultCode == RESULT_OK) { 
      // Fill the list view with the strings the recognizer thought it 
      // could have heard 
      ArrayList<String> matches = data 
        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
      mList.setAdapter(new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, matches)); 
      //matches is the result of voice input. It is a list of what the user possibly said. 
      //Using an if statement for the keyword you want to use allows the use of any activity if keywords match 
      //it is possible to set up multiple keywords to use the same activity so more than one word will allow the user 
      //to use the activity (makes it so the user doesn't have to memorize words from a list) 
      //to use an activity from the voice input information simply use the following format; 
      //if (matches.contains("keyword(s) here") { startActivity(new Intent("name.of.manifest.ACTIVITY") 

      if (matches.contains("information")) { 
       startActivity(new Intent("android.intent.action.INFOSCREEN")); 
      } 

      if (matches.contains("home")) { 
       startActivity(new Intent("android.intent.action.MENU")); 
      } 

     } 

     super.onActivityResult(requestCode, resultCode, data); 

    } 
} 











package com.example.com.proto1; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 
import android.speech.RecognizerIntent; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.speech.tts.TextToSpeech; 
import java.util.ArrayList; 
import java.util.List; 

public class menu extends Activity implements TextToSpeech.OnInitListener { 

    MediaPlayer sep, aep, vpm; 

    TextToSpeech mTts; 

    public void onInit(int i) { 
     // TODO Auto-generated method stub 
     mTts.speak("EyePhone Main Menu", TextToSpeech.QUEUE_FLUSH, null); 

    } 

    @Override 
    protected void onCreate(Bundle aboutmenu) { 
     // TODO Auto-generated method stub 
     super.onCreate(aboutmenu); 
     setContentView(R.layout.mainx); 


     // Setting up the button references 
     Button info = (Button) findViewById(R.id.aboutbutton); 
     Button voice = (Button) findViewById(R.id.voicebutton); 
     Button speakButton = (Button) findViewById(R.id.btn_speak); 

     info.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("android.intent.action.INFOSCREEN")); 

      } 
     }); 

     voice.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       try { 
        Intent voiceIntent = new Intent(
          "android.intent.action.RECOGNITIONMENU"); 
        startActivity(voiceIntent); 
       } catch (Exception e) { 

       } 
      } 
     }); 

     speakButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       try { 

        //this is the place where I thought the code should go that I am asking about 

       } catch (Exception e) { 

       } 
      } 

     }); 

    } 

} 

답변

0

새로운 클래스 파일에 STATIC 방법을 정의는 (필요한 경우 등) 활동을 매개 변수로 참조를 걸립니다 (TTSHelper 말할 수 있습니다). 이제 버튼의 onClick 메서드에서이 정적 메서드를 호출합니다. 이렇게하면 각 활동 클래스에서 코드를 반복하지 않아도됩니다.

정의 등 :

public class TTSHelper { 
    public static void myMethod(Activity activity){ 
     //do whatever you want 
    } 
} 

사용 등 :

public void onClick(View v) { 
    TTSHelper.meMethod(this); 
} 

P.S. Activity 참조를 Context로 매개 변수로 대체 할 수 있지만 전달해야하는 컨텍스트 참조 (작업/응용 프로그램)를 알고 있어야합니다.

+0

이것은 나를 위해 작동하지 않습니다. 코드를 사용하여 예제를 줄 수 있습니까? –